0
xdate: 2013-10-26  = 
Fri Oct 25 2013 20:00:00 GMT-0400 (Eastern Daylight Time)

xdate: Oct 26, 2013  = 
Sat Oct 26 2013 00:00:00 GMT-0400 (Eastern Daylight Time)

xdate: 10/26/2013  =
Sat Oct 26 2013 00:00:00 GMT-0400 (Eastern Daylight Time)

xdate: Oct. 26, 2013  =
Sat Oct 26 2013 00:00:00 GMT-0400 (Eastern Daylight Time)

When I create a date in javascript using new Date(xdate) with the strings above, they all seem to work fine except for yyyy-mm-dd which comes out to 8PM the night before. Unfortunately that is the format created by the HTML5 type=date. Any way to have that string create a normal date also.

I guess my question is more: WHY does that one format create a date that is 4 hours off. I create a date using 2013-10-26 then do a getDate and get the 25th. that seems wrong!!

2 Answers2

1

comes out to 8PM the night before

No, your datetime does not come out to 8PM the night before. Notice that the date you create is UTC date. The reason you see that is because your local time zone is 4 hours before UTC (GMT-4). That's just the presentation of the date calculated based on your local time zone, internally, date time is represented by the number of miliseconds since 01/01/1970 00:00:00 UTC.

You could get the internal representation of the date using Date.getTime() which will return the same value on all timezones. For example, with:

var date = new Date("2013-10-26");
  • When your local time zone is GMT-4: You will see the presentation is: Fri Oct 25 2013 20:00:00 GMT-0400

  • When your local time zone is GMT+7. You will see: Sat Oct 26 2013 07:00:00 GMT+0700

But date.getTime() returns the same value. That's the value you should care about.

Khanh TO
  • 48,509
  • 13
  • 99
  • 115
0

You can create your data like this and avoid this timeone offset issue:

xdate = '2013-10-26';
var date = new Date(xdate.replace(/-/g, ','));

Testing:

var d1 = new Date(xdate); // Fri Oct 25 2013 20:00:00 GMT-0400 (EDT)

var d2 = new Date(xdate.replace(/-/g, ',')); // Sat Oct 26 2013 00:00:00 GMT-0400 (EDT)

Notice difference in date values between d1 and d2.

d2 is showing correct value of Oct 26 2013 because of this - to , replacements.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • this will create the same result as the date is still created in UTC http://jsfiddle.net/gURGG/. Try it with your local time zone. – Khanh TO Oct 26 '13 at 10:40
  • @KhanhTO: I have added **Testing** section above so that you can see difference between 2 outputs. – anubhava Oct 26 '13 at 10:44
  • @KhanhTO: Your [jsfiddle](http://jsfiddle.net/gURGG/1/) is also showing 2 different dates. 1st one with replace is: `Sat Oct 26 2013 00:00:00 GMT-0400 (EDT)` but 2nd with with default is: `Fri Oct 25 2013 20:00:00 GMT-0400 (EDT)` that verifies my result. – anubhava Oct 26 '13 at 10:50
  • Sorry, I deleted that comment. This solution may be what the OP is looking for, but he should not as this could create problems if the code is run on clients with different timezones as `the actual value of date (date.getTime())` is different. – Khanh TO Oct 26 '13 at 10:54
  • @KhanhTO: Yes if OP wants to avoid web browsers in different timezones then it is always better to perform date/time calculations on server side. – anubhava Oct 26 '13 at 11:03
  • "*`new Date(xdate.replace(/-/g, ','))` will create date as `new Date(year, month, date)`*" - No, it won't. It still will be `new Date(datestring)`, and it's up to the browser how (and in which timezone) he parses it – Bergi Oct 26 '13 at 11:03
  • @Bergi: Thanks, that was corrected. However `new Date("2013,10,26");` does correct the stated problem as compared to: `new Date("2013-10-26");` – anubhava Oct 26 '13 at 11:09
  • unfortunately, some browsers handle the type=date differently and I don't always get back the yyyy-mm-dd. Since I am only using dates (not times) I just subtracted the hours from 24 and added it back to the date. I got that from some stackoverflow comments. Thanks. – user2847999 Oct 26 '13 at 14:42
  • @user2847999: If you can get `year, month, day` separately then also `new Date(year, month, date)` will work fine to avoid this problem. – anubhava Oct 26 '13 at 15:11