DataJS and date

Jun 15, 2012 at 8:38 PM

Hi, guys!

I have a problem with save of data through datajs. A lot of datepicker controls, in cases when time is not necessary (in most cases!!!), use date only, without time. But in this case datajs converts date incorrectly because it converts all datetime values in UTC format. For example, I have the next date:

Fri Dec 18 1992 00:00:00 GMT+0200 (FLE Standard Time)

It means that my control shows me 18 Dec1992 and time is not needed at all. Of course, my GMT is +2 hours, but it doesn't important for me. And datajs, anyway, converts my date value using this GMT. I mean the result is:

1992-12-17T22:00:00 (my date value minus 2 hours)

It's absolutely strange behaviour and I can't find the right solution how to set datajs to not do that conversion. Yes, I can write a hack trick over the control (for example, I use native Sencha Touch 2 controls), but it's not right method.

The question is: how to work with dates using datajs correctly?

Jun 17, 2012 at 8:35 AM

Or how to make datajs to not convert date in UTC date?

Jun 21, 2012 at 8:42 AM

Hi StasKalishenko,

  DataJS uses UTC for the DateTime and DateTimeOffset values so that the values can be manipulated directly from the wire using Javascript's UTC APIs without having to worry about local time zone.  Displaying date values received from the wire to a particular time zone is something that has to handled by the presentation layer of the application. 

Now, based on your example above, is that how the control is displaying the date value? And what you would like is to have it show the date value in the current timezone or the specific time zone (in the example above +0200).   If the former, then the control has to use javascript locale aware functions instead of the UTC ones.. perhaps it has some knob to change this behavior.  If you want the latter, then you will have to make that adjustment yourself (or the datepicker control) because Javascript only allows to get the date values either in UTC or in the current locale. For this adjustment you can take advantage of the __offset field that DataJS adds to the resulting Date object when dealing with DateTimeOffset values.   For more background information you can look at this entry in Marcelo's blog and this other one in his blog also that talks about UTC and timezone nuisances with Date objects in javascript. 

Finally, if you are using ATOM, then it is not possible to disable DataJS parsing of Dates, that is because ATOM payloads clearly state when a property is of type Edm.DateTime or Edm.DateTimeOffset.   On the other hand if using JSON then there is no parsing of dates by default.. this is because JSON payloads do not necessarily differentiate between a DateTime value and a string value that happens to be formatted as a DateTime literal.  The behavior can be changed by setting the recognizeJsonDates flag in the OData.jsonHandler object to true or providing a metadata object that clearly identifies the property as of type Edm.DateTime.   Keep in mind that in JSON your date values will come as strings following this format  /Date(mmmmmm[+- nn])/  where mmmm is the number of miliseconds and +-nn is the optional offset information.  You will have to convert this values your self and in Javascript the Date(number) constructor creates a Date object using UTC.

Regards,

Alex Trigo