Umesh
Umesh

Reputation: 115

How to convert datetime in seconds ANGULAR JS

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.

Upvotes: 0

Views: 3200

Answers (2)

Prashant Tapase
Prashant Tapase

Reputation: 2147

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);

Upvotes: 1

Wayne Ellery
Wayne Ellery

Reputation: 7958

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

Upvotes: 3

Related Questions