简体   繁体   中英

javascript new Date(date) return wrong day

i was trying to confront tree javascript Date objects with this sintax

 var from = new Date(1900,0,0);
 var to = new Date();
 var dataTicket = new Date(dataString);

     if (dataTicket > from && dataTicket < to) {
     alert("OK");
     }

but i can't get this working because the dataTicket continue to give me the wrong day!

when i check in the console i have this values in the if statement:

dataString = "Tue Sep 10 2013 22:44:07 GMT 0200 (ora legale Europa occidentale)"

from = "Sun Dec 31 1899 00:00:00 GMT+0100 (ora solare Europa occidentale)"

to= "Tue Sep 10 2013 23:32:44 GMT+0200 (ora legale Europa occidentale)"

and here come the strange thing:

dataTicket = "Wed Sep 11 2013 00:44:07 GMT+0200 (ora legale Europa occidentale)"

i can get it working because dataTicket value is one day after it's assignation, any clue about this?

Your dateString has a broken zone part. For the RFC2822, the zone should have a + or - , but yours doesn't and then it's interpreted as UTC (+0000); in fact the time is 00:44:27 instead of 22:44:27.

dataString = "Tue Sep 10 2013 22:44:07 GMT+0200 (ora legale Europa occidentale)"

This dataString will work as expected (note the + sign)

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