开发者

My date is always one hour out - Parsing a JSON date

开发者 https://www.devze.com 2023-03-02 15:31 出处:网络
I live in GMT and it\'s +1 daylight savings at the moment, which is probably relevant... I have a webservice, and it pulls down a date which ends up in this format

I live in GMT and it's +1 daylight savings at the moment, which is probably relevant...

I have a webservice, and it pulls down a date which ends up in this format

'\/Date(1234567)\/'

In order to find out w开发者_JAVA百科hat this date is, I simply Eval it after removing the slashes.

Here's an example:

var jsonDate = 'Date(1304515498497)';

alert (new Date(1304515498497).toString() + "\n" +
           Date(1304515498497).toString()  + "\n" +
            eval(jsonDate));      

http://jsfiddle.net/yPCNE/1/

If I put new in front of it, the date changes, and it is incorrect. However, in my code, if I remove New, I always get today's date. This may be a consequence of using the datejs library. From what I can see, doing eval('new ' + date) is resulting in the time being exactly one hour behind, but alert()ing both of the dates shows they are both set to +1 GMT and are identical except for the one hour. I can't fathom where it's coming from.

Is there some better way to do this? I just want to convert the incoming JSON date into a regular date without any +1s messing it up.


If you don't put new in front of it, Date().toString() will just return the current time, whatever you put inside the brackets.

alert (Date(1).toString() + "\n" +
       Date(42).toString()  + "\n" +
       Date("banana").toString());  //All return current time
0

精彩评论

暂无评论...
验证码 换一张
取 消