简体   繁体   中英

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. 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

Interesting function you have here. I see at least one problem dateNum is not a string.

Might be a good idea to submit what you expect to get from your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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