如何转换日期时间以秒为单位ANGULAR JS

时间:2015-08-01 07:50:31

标签: angularjs datetime

我在控制器中从JSON获取数据,需要将其转换为秒。

[
    {
        "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"
    }
]

我想要实现的是在几秒钟内获得"ExecutionStart" and "ExecutionEnd"之间的差异。

我已经在互联网上寻找解决方案,但遗憾的是找不到任何解决方案。所以我没有表明我已经尝试过了。

请帮助。

2 个答案:

答案 0 :(得分:2)

首先从日期字符串创建Date对象:

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

然后减去日期并除以1000,因为差异将以毫秒为单位,并且一秒钟内有1000毫秒。

var diffSeconds = (executionEnd - executionStart) / 1000;

JsFiddle

答案 1 :(得分:1)

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

的解决方案
BigQuery