PDA

View Full Version : How to parse the date in Ext.data.Model?



dmitry.akatev
29 Jun 2015, 11:49 PM
My English is bad. I have a problem with sorting dates:


I have a model:

Ext.define('SA.model.Users', {
extend: 'Ext.data.Model',


fields: [
{ name: 'id' },
{ name: 'userName' },
{ name: 'created', type: 'date'}, //!!!!!!!important
//...
],


idProperty: 'id',
//...
});


I load Store, And I get data:

[{
//fields
created: '2015-06-30T06:19:11.8765252Z' //30.06.2015 09:06:11 (format: 'd.m.Y H:m:s')
}, {
//fields
created: '2015-06-30T06:17:34.8607629Z' //30.06.2015 09:06:34 (format: 'd.m.Y H:m:s')
}]
30.06.2015 09:06:11 < 30.06.2015 09:06:34


I do not work correctly sorting:

// var store = findStore
var d1 = store.findRecord('id', 1).get('created'); //2015-06-30T06:19:11.8765252Z - 30.06.2015 09:06:11
var d2 = store.findRecord('id', 2).get('created'); //2015-06-30T06:17:34.8607629Z - 30.06.2015 09:06:34


var compare_d1_and_d2 = d1 < d2 ? 'd1 < d2' : 'd1 > d2';
console.log(Ext.Date.format(d1, 'd.m.Y H:m:s')); //30.06.2015 09:06:11
console.log(Ext.Date.format(d2, 'd.m.Y H:m:s')); //30.06.2015 09:06:34
console.log(compare_d1_and_d2); //d1 > d2 !!! it is not right


How do I make:

Ext.define('SA.model.Users', {
extend: 'Ext.data.Model',


fields: [
{ name: 'id' },
{ name: 'userName' },
{ name: 'created', type: 'date', convert: function (v) {
return new Date(Ext.Date.format(new Date(v), 'Y-m-d H:m:s')); //!!!!!!
}
},
//...
],


idProperty: 'id',
//...
});



// var store = findStore
var d1 = store.findRecord('id', 1).get('created'); //2015-06-30T06:19:11.8765252Z - 30.06.2015 09:06:11
var d2 = store.findRecord('id', 2).get('created'); //2015-06-30T06:17:34.8607629Z - 30.06.2015 09:06:34


var compare_d1_and_d2 = d1 < d2 ? 'd1 < d2' : 'd1 > d2';
console.log(Ext.Date.format(d1, 'd.m.Y H:m:s')); //30.06.2015 09:06:11
console.log(Ext.Date.format(d2, 'd.m.Y H:m:s')); //30.06.2015 09:06:34
console.log(compare_d1_and_d2); //d1 < d2 !!! it is right!!!




This is code correct?

convert: function (v) {
return new Date(Ext.Date.format(new Date(v), 'Y-m-d H:m:s')); //!!!!!!
}

tristan.lee
30 Jun 2015, 10:02 AM
I'm not sure that I follow. What's the reason for converting a date into a date?

dmitry.akatev
2 Jul 2015, 12:53 AM
I have a lot of dates, but for example, I will use 2.

var string_date1 = '2015-06-30T06:17:34.8607629Z';
var string_date2 = '2015-06-30T06:19:11.8765252Z';


And I need to sort them.


On the screen, they are displayed in this format: 'd.m.Y H:m:s'
first date displayed: "30.06.2015 09:06:34" //string date = '2015-06-30T06:17:34.8607629Z'
second date displayed: "30.06.2015 09:06:11" //string date = '2015-06-30T06:19:11.8765252Z'


When I want to sort them, they should change location, because 11 is less than 34:
1) "30.06.2015 09:06:11" - The second date was the first
2) "30.06.2015 09:06:34" - The first date was the second


But it does not occur:

date1 = new Date(string_date1); //30.06.2015 09:06:34
date2 = new Date(string_date2); //30.06.2015 09:06:11
console.log(date1 < date2 ? 'yes' : 'no');
that is 34 seconds less than 11?

jdkhamba
2 Jul 2015, 3:50 AM
Looks like you may be missing the dateFormat Config while defining the model:

http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.data.Field-cfg-dateFormat

So your model should look something like this:


Ext.define('SA.model.Users', {
extend: 'Ext.data.Model',




fields: [
{ name: 'id' },
{ name: 'userName' },
{ name: 'created', type: 'date', dateFormat:'Y-m-dTH:i:s'},
//...
],




idProperty: 'id',
//...
});


The dateFormat would really depend upon the data that is loaded in the store.Take a look at various date formats that might fit your needs:

http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.Date

dmitry.akatev
2 Jul 2015, 5:42 AM
It does not work, because I bad understand. I made the first(my) version:
52781
link:
https://fiddle.sencha.com/#fiddle/pqc

tristan.lee
2 Jul 2015, 12:46 PM
In your fiddle, it looks like the 2nd grid is sorting how you want, correct?