简体   繁体   English

为什么我不能从给定的字符串创建一个新的Date()?

[英]why I can't create a new Date() from given string?

I'm using moment.js to find a time in different timestamp. 我正在使用moment.js在不同的时间戳中查找时间。 I wrote a simple javascript: 我写了一个简单的javascript:

$(function () {
    var timestamp = 1443556318; //GMT Tue, 29 Sep 2015 19:51:58 GMT
    var today2 = moment.unix(timestamp).tz('America/New_York').toString();
        today = new Date(today2);
        alert(today2);
        alert(today);

        var hh = today.getHours();
        alert(hh); //why it shows me 21 instead of 15?   
});

and seems like this line today = new Date(today2); 似乎today = new Date(today2);这行today = new Date(today2); does not work properly. 不能正常工作。 Can you help me with that? 你能帮我吗?

http://jsfiddle.net/b8o5cvdz/3 http://jsfiddle.net/b8o5cvdz/3

It doesn't work because, you use the following constructor 它不起作用,因为您使用以下构造函数

new Date(dateString);

where the 在哪里

String value representing a date. 表示日期的字符串值。 The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601). 该字符串应采用Date.parse()方法可识别的格式(符合IETF的RFC 2822时间戳以及ISO8601的版本)。

If you want to all the possible constructors, please have a look here . 如果您想使用所有可能的构造函数,请在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM