ExtJS and Updating old dates

LabKey Support Forum (Inactive)
ExtJS and Updating old dates trent  2013-08-06 04:03
Status: Closed
 
I have an ExtJS form.

Is there a good way to handle old dates? Should I use some particular function to get the value?

In the form, I use the code: dob = form.findField('date_of_birth').getValue(); This always prints out the correct date.

Use case 1: 5/4/1971 (dd/mm/yyyy) (Works fine)

above call logs: Mon Apr 05 1971 00:00:00 GMT+1000 (AUS Eastern Standard Time)
date field sent in HTTP request as: 1971-04-04T14:00:00.000Z
resulting save data: 1971/04/05 00:00:00

Use case 2: 4/4/1971 (dd/mm/yyyy/ (Does not work fine)

above call logs: Sun Apr 04 1971 00:00:00 GMT+1100 (AUS Eastern Daylight Time)
date field sent in HTTP request as: 1971-04-03T13:00:00.000Z
resulting save data: 1971/04/03 23:00:00

Is there a good way to handle old dates? I also tried passing in: Ext4.Date.parse(form.findField('date_of_birth').rawValue, 'd/m/Y') but to no avail.

Appreciate any advice :)
 
 
trent responded:  2013-08-06 04:42
Ok, looks like the following will work:

Ext4.Date.format(Ext4.Date.parse(dateField.rawValue, 'd/m/Y'), 'Y-m-d')
 
Nick Kerr responded:  2013-08-06 10:01
Hi Trent,

When using Ext4.Date.parse it acts as a matching function which will return a Date object if the input value matches the date format supplied. This might have been why it did not work with "dd/mm/yyyy" since the input might not have been in this format.

If you are looking to get a Date object I'd recommend handing the input value to a new Date object (e.g. new Date(inputValue); ). ExtJS provides a lot of utility functions for dealing with Date objects (see all the methods on Ext.Date) and using Ext.Date.format can give you the output string you are looking to display.

Thanks,
Nick
 
trent responded:  2013-08-06 15:14
Hi Nick,

Thanks - yeah I also tried that. It still goes to a day before. the getValue function returns a date, and when i was logging it to the console, it's printing the correct date.

As an additional check (away from ext):

LABKEY.Query.updateRows({
    schemaName: 'lists',
    queryName: 'aa',
        rows: [{
            'dd': new Date(1971,1,1),
            'Key': 6
        }],
    success: function () { alert('saved'); }
    });

list named, aa
date field named, dd

This should be saved as 1st Feb 1971, but is saved as 31st Jan 1971.

If I set a date between 1st January and 4th of April (inclusive), to years 1987 or before, it seems to save as the date before. All other cases save the correct date. Odd, right?

Cheers,
Trent
 
Nick Kerr responded:  2013-08-07 11:24
Hi Trent,

Thanks for posting the code snippet. After investigating a bit, I've found the reason why your dates are getting transformed. In the encoding of Ajax requests (LABKEY.Query.updateRows ends up calling Ext.Ajax.request) the JSON.stringify method is called on the Date object. When JSON.stringify(Date) is used it calls Date.prototype.toISOString which returns the string of the UTC date. Here is an example of the differences in using JSON.stringify:

d = new Date(1971,1,1);
js = JSON.stringify;

console.log("date:", js(d));
console.log("toString:", js(d.toString()));
console.log("toISOString:", js(d.toISOString()));

// Results
// date: "1971-02-01T08:00:00.000Z"
// toString: "Mon Feb 01 1971 00:00:00 GMT-0800 (Pacific Standard Time)"
// toISOString: "1971-02-01T08:00:00.000Z"

As a result the UTC string version of the date you supply is being posted to the server. I think the ideal way dates are stored on a server is UTC so that all dates are timezone independent, but in most cases the local time ends up being stored. If you want to post the local time to the server I'd recommend calling toString() on the Date or converting it using the Date.format functions made available in Ext.

Thanks,
Nick