Need to return the date and time attributes without UTC conversion in dojox/grid/DataGrid

4173
4
Jump to solution
12-05-2014 07:24 AM
GwynnHarlowe
New Contributor II

Hope someone can help me.  My web page is displaying data from a related table using dojox/grid/DataGrid.

 

I found a format script that formats it as an actual date and time:

 

//format the date and time that is used in the related table

function formatDate(value) {

     var inputDate = new Date(value);

     return dojo.date.locale.format(inputDate, { 

               selector: 'date',

               datePattern: 'MM/dd/yyyy'

     });

}

function formatTime(value) {

     var inputDate = new Date(value);

     return dojo.date.locale.format(inputDate, { 

               selector: 'time',

               timePattern: 'hh:mm a' 

     });

}

 

In my table I call it like this:

               <th width="100" field="StatusDate" formatter="formatDate"><em>Date</em></th>

              <th width="100" field="StatusTime" formatter="formatTime"><em>Time</em></th>

 

This works to get a readable date and time but it assumes it is UTC and the browser converts it to Mountain Standard Time (I am in Arizona).

 

Is there a way to prevent this so that it displays the date and time as they are in the attribute fields?

 

Thanks

 

Gwynn

0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

I have been using moment.js for date/time formatting in web applications and it is great. You should be able to wrap moment.js formatting functions inside the formatDate() and formatTime() functions.

View solution in original post

0 Kudos
4 Replies
GwynnHarlowe
New Contributor II

OK, if nobody knows how to do this  then is there another way to format attribute date and time fields when using dojox/grid/DataGrid?

Thanks in advance/

0 Kudos
OwenEarley
Occasional Contributor III

I have been using moment.js for date/time formatting in web applications and it is great. You should be able to wrap moment.js formatting functions inside the formatDate() and formatTime() functions.

0 Kudos
OwenEarley
Occasional Contributor III

As to why this is happening - see here:

Creates a JavaScript Date instance that represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.

The date constructor assumes the input to be UTC. There is nowhere in your code that is telling Dojo that your incoming dates are in MST. Dojo appears to be applying an offset to get from UTC to MST based on your browser settings.

However, you may be able to simplify your formatDate and formatTime functions by removing the dojo.date.locale.format() and replacing it with a simple moment.js date formatting function. This way the code just assumes input/output values are UTC even though they are actually MST and the results should be displayed as they are in the attribute fields.

0 Kudos
GwynnHarlowe
New Contributor II

Thank you very much.  moment.js was just the ticket.  This is how I used it:

function formatDate(value) {  

       var inputDate = new Date(value);   

            return moment.utc(inputDate).format("M/D/YYYY");

}

function formatTime(value) {  

     var inputDate = new Date(value);  

     return moment.utc(inputDate).format('hh:mm a');

}

This forces it to display the date and time as it is in the attribute field.

0 Kudos