10

I am having a issue with my DS.Model parsing dates in the format of "YYYY-MM-DD". They are always one day behind.

Here is an example:

http://jsfiddle.net/ZUV8v/

Using Date objects on the console I get similar results

> new Date('2012-09-20')
Wed Sep 19 2012 17:00:00 GMT-0700 (PDT)

Is this a ember bug or a javascript bug or a Chrome bug or am I missing something?

Chrome Version 21.0.1180.89 on OSX 10.7

Aaron Renoir
  • 4,283
  • 1
  • 39
  • 61
  • Are you still able to reproduce this with your JS fiddle? I get the following result, and it seems correct: `date: Thu Sep 20 2012 03:00:00 GMT+0300 (Arabic Standard Time)`. – s.ermakovich Sep 21 '12 at 07:02
  • I believe the date is correct for you because of the time zone you are in. So like @jasolko said Javascript fills in the missing values with zeros and then evaluates it based on the current time zone. In my case being in the Pacific time zone subtracts 7 hours moving the date to the previous day. – Aaron Renoir Sep 24 '12 at 21:09

1 Answers1

4

I ran into this just the other day.

According to the ECMAScript Specification 15.9.1.15

All numbers must be base 10. If the MM or DD fields are absent "01" is used as the value. If the HH, mm, or ss fields are absent "00" is used as the value and the value of an absent sss field is "000". The value of an absent time zone offset is "Z".

new Date('2012-09-20')

is the same as

new Date("2012-09-20T00:00:00.000Z")

The console then outputs the value in your local timezone.

Jason P
  • 1,415
  • 14
  • 12
  • ok that makes sense. Do you think ember-data should parse the date differently or should the json date value always include time and timezone? – Aaron Renoir Sep 21 '12 at 20:20
  • Sorry I don't typically look here over the weekend. I think it makes sense to always include the timezone. I only ever write in-house apps that stay in a single timezone and have never had to think of this before. – Jason P Sep 24 '12 at 14:09
  • no prob. If it is a datetime object I agree but it seems there is not a good way do do date only objects in javascript. I started a new question regarding serialization. http://stackoverflow.com/questions/12538242/serialize-date-attributes . Thanks for the help. – Aaron Renoir Sep 24 '12 at 21:01