在javascript中将iso日期转换为毫秒 [英] convert iso date to milliseconds in javascript

查看:156
本文介绍了在javascript中将iso日期转换为毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将iso日期转换为毫秒吗?
例如我想转换此iso

Can I convert iso date to milliseconds? for example I want to convert this iso

2012-02-10T13:19:11+0000

到毫秒。

因为我想比较当前从创建日期开始的日期。并且创建的日期是iso日期。

Because I want to compare current date from the created date. And created date is an iso date.

推荐答案

试试这个

var date = new Date("11/21/1987 16:00:00"); // some mock date
var milliseconds = date.getTime(); 
// This will return you the number of milliseconds
// elapsed from January 1, 1970 
// if your date is less than that date, the value will be negative

编辑

你'我们提供了ISO日期。它也被 Date 对象的构造函数接受

You've provided an ISO date. It is also accepted by the constructor of the Date object

var myDate = new Date("2012-02-10T13:19:11+0000");
var result = myDate.getTime();

编辑

我发现最好的是手动摆脱偏移量。

The best I've found is to get rid of the offset manually.

var myDate = new Date("2012-02-10T13:19:11+0000");
var offset = myDate.getTimezoneOffset() * 60 * 1000;

var withOffset = myDate.getTime();
var withoutOffset = withOffset - offset;
alert(withOffset);
alert(withoutOffset);


似乎有效。至于将ISO字符串转换为日期对象的问题,您可以参考提供的链接。

​ Seems working. As far as problems with converting ISO string into the Date object you may refer to the links provided.

编辑

根据Prasad19sara的评论修正了错误转换为毫秒的错误。

Fixed the bug with incorrect conversion to milliseconds according to Prasad19sara's comment.

这篇关于在javascript中将iso日期转换为毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆