0

I am fetching data from JSON in my controller and need to convert it into seconds.

[
    {
        "Id": "0",
        "Name": "Subscriber",
        "ItemsCount": 5,
        "ItemsFailedCount": 6,
        "ExecutionStart": "2015-08-01T08:01:00.9748076+01:00",
        "ExecutionEnd": "2015-08-01T08:01:00.9748076+01:00"
    }
]

What I am trying to achieve is getting the difference between "ExecutionStart" and "ExecutionEnd" in seconds.

I have looked upon for the solution of this on internet and couldn't find any solution unfortunately. So I have nothing to show that I've tried.

Kindly help.

1

2 Answers 2

3

First create Date objects from the date strings:

var executionStart = new Date(data[0].ExecutionStart);
var executionEnd = new Date(data[0].ExecutionEnd);

Then subtract the dates and divide by 1000 because difference will be in milliseconds and there are 1000 milliseconds in a second.

var diffSeconds = (executionEnd - executionStart) / 1000;

JsFiddle

Sign up to request clarification or add additional context in comments.

Comments

1

solution using date.parse();

var data = [
    {
        "Id": "0",
        "Name": "Subscriber",
        "ItemsCount": 5,
        "ItemsFailedCount": 6,
        "ExecutionStart": "2015-08-01T08:01:00.9748076+01:00",
        "ExecutionEnd": "2015-08-01T08:01:10.9748076+01:00"
    }
];


var executionStarts = Date.parse(data[0].ExecutionStart);
var executionEnds =  Date.parse(data[0].ExecutionEnd);

var diffSeconds = (executionEnds - executionStarts)/1000;

alert(diffSeconds);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.