4
votes

I have following text field with dojo dojo Date text type.

dojoType="dijit.form.DateTextBox" required="true"/>

I use the following code to set the dijit.form.DateTextBox value

dijit.byId('dtinv').attr('value',new Date(tList[i].getAttribute("dtinv"))) ;

tList[i].getAttribute("dtinv") has value 2010-04-02

When the value is set, I see 4/1/2010. Always the date in dojo date box shows one day less. Is there something I should take care?

1

1 Answers

5
votes

Dojo and Dijit process Dates in local time, not GMT, which itself may have been a poor design choice, but given this, if you use dojo.date.stamp.fromISOString() or pass the string "2010-04-02" as the value attribute in your HTML you will get April 2 at midnight in your local time. Pass that to the Dijit and it will be happy. Using the new ES5 Date constructor in Javascript will have different results:

(I'm in the Eastern timezone)

>>> new Date("2010-04-02")
Thu Apr 01 2010 20:00:00 GMT-0400 (EST) {}

that defines a date in GMT. However, the following object would work as expected with Dijit:

>>> new Date(2010, 3, 2)
Fri Apr 02 2010 00:00:00 GMT-0400 (EST) {}

Also, Javascript's new Date constructor is poorly defined when it comes to taking Strings. I'm not sure if the result you're getting is consistent across all browsers. You are advised to use dojo.date.stamp.fromISOString("2010-04-02") to get the corresponding Date object, or ES5 ISO date methods, where available in newer browsers.