简体   繁体   English

日期不会在jsFiddle中发出警报

[英]Date does not alert in jsFiddle

var dateNum = Number('/Date(1306348200000)/'.replace(/[^0-9]/g,''));
var formattedDate = new Date(parseInt(dateNum.substr(6)));   
alert(formattedDate);

What's wrong with this code? 此代码有什么问题? Why does it not execute and give me the desired result... 为什么它不执行并给我想要的结果...

Try this. 尝试这个。

var formattedDate = new Date(parseInt(dateNum.toString().substr(6)));

Felix's comment is the answer :) 费利克斯的评论就是答案:)

I've no idea why you're doing it in a complicated way - / / is for regular expressions, not for dates. 我不知道您为什么要以复杂的方式进行操作- / /用于正则表达式,而不用于日期。 Then you also have this notation inside a string. 然后,您在字符串中也有此表示法。 I'm not aware of any /Date(...)/ format. 我不知道任何/Date(...)/格式。 What you're doing on the first line is parsing the number out of it, but why not do it yourself? 您在第一行所做的就是从中解析出数字,但是为什么不自己做呢?

This works fine: 这很好用:

var formattedDate = new Date(1306348200000);   
alert(formattedDate);

To format it, you would need certain functions to combine the date components as described here: 要格式化它,您将需要某些功能来组合日期组件,如下所述:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date#Methods_2 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date#Methods_2

Interesting function you have here. 您在这里拥有有趣的功能。 I see at least one problem dateNum is not a string. 我看到至少一个问题dateNum不是字符串。

Might be a good idea to submit what you expect to get from your code. 提交您希望从代码中得到的东西可能是一个好主意。

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

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