新日期(毫秒)返回日期无效 [英] new Date(milliseconds) returns Invalid date
问题描述
新日期(毫秒);我正在尝试使用javascript将毫秒转换为日期。
构造函数,但是当我给它一个毫秒值为1372439683000它返回无效的日期。如果我转到将毫秒转换为ms的站点,则返回正确的日期。
任何想法为什么?
你没有使用号码,您正在使用一个看起来像一个数字的字符串。根据MDN,当您将字符串传递到 Date
,它希望
解析方法识别的格式(IETF-符合RFC 2822时间戳)。
这样一个字符串的一个例子是 1995年12月17日03:24 :00
,但您传递的字符串看起来像 1372439683000
,无法解析。
将毫秒
转换为使用 parseInt
或一元 +
:
新日期(+毫秒);
new Date(parseInt(Milliseconds,10));
I am trying to convert milliseconds to a date using the javascript using:
new Date(Milliseconds);
constructor, but when I give it a milliseconds value of say 1372439683000 it returns invalid date. If I go to a site that converts milliseconds to date it returns the correct date.
Any ideas why?
You're not using a number, you're using a string that looks like a number. According to MDN, when you pass a string into Date
, it expects
a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).
An example of such a string is "December 17, 1995 03:24:00
", but you're passing in a string that looks like "1372439683000
", which is not able to be parsed.
Convert Milliseconds
to a number using parseInt
, or a unary +
:
new Date(+Milliseconds);
new Date(parseInt(Milliseconds,10));
这篇关于新日期(毫秒)返回日期无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!