Friday 5 April 2013

How to format date and time in Sencha Touch 2

Sencha Touch 2 provides lot of utility methods for managing date and time information using Ext.DateExtras Object. Ext.DateExtras class will not be included by default in Sencha Touch 2 Application. You need to add this class as the requires in which you wish to use them (Either Controller or Application). Inorder to use Ext.DateExtras, you need to use Ext.Date Object.

We are going to use format() function for fomating current date and time in various ways. This method accepts date and format as parameters and returns the formatted date string.
Ext.application({
    requires: [         'Ext.DateExtras'     ],     name: 'MyApp',     launch: function() {         var sampleDate = new Date();         var formats = ['Y-m-d H:i:s', 'Y-m-d', 'l, F d, Y g:i:s A','l, F d, Y', 'n/j/Y', 'g:i:s A', 'Y-m-d\TH:i:s'];         console.log("Formated Dates:");         Ext.each(formats,function(format){             var formated = Ext.Date.format(sampleDate,format);             console.log(format+': '+formated);         });     } });
When i run this code in Google Chrome, Following are the output

Formated Dates:
Y-m-d H:i:s: 2013-02-01 16:54:47
Y-m-d: 2013-02-01
l, F d, Y g:i:s A: Friday, February 01, 2013 5:13:22 PM
l, F d, Y: Friday, February 01, 2013
n/j/Y: 2/1/2013
g:i:s A: 4:54:47 PM
Y-m-dTH:i:s: 2013-02-01IST17:02:46

Important Note: For the list of available various date Formats, Please click here

Hope, you enjoyed this Post.

No comments:

Post a Comment