Skip to main content

ExtJs 4 convert String to Date Example

Your date may be a numeric value or String data as a JSON response to a Server request. Now having that value converted to a date has its advantages. All you have to do is check the object if it has the getMonth() method. If it does that means its already a date object otherwise just use the parser along the date format. Here is how to do it
?
1
2
3
4
5
6
7
8
9
function convertMyDate(value) {
    //check if the input value is already a date
    if(!value.getMonth){
        //if not a date then convert it to date
        //specify the format, so if the value is 20120229 then use Ymd format
        value = Ext.Date.parse(value,'Ymd');
    }
    return  value;
}

Ext.Date.parse( String input, String format, [Boolean strict] ) : Date

Parses the passed string using the specified date format. Note that this function expects normal calendar dates, meaning that months are 1-based (i.e. 1 = January). Keep in mind that the input date string must precisely match the specified format string in order for the parse operation to be successful (failed parse operations return a null value).

Examples: 
?
1
2
myDate = Ext.Date.parse("2012-02-28", "Y-m-d");
myDate = Ext.Date.parse("2012-01-03 5:43:21 PM", "Y-m-d g:i:s A");



Some of the currently supported date formats ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Format  Description                                                               Example returned values
------  -----------------------------------------------------------------------   -----------------------
  d     Day of the month, 2 digits with leading zeros                             01 to 31
  D     A short textual representation of the day of the week                     Mon to Sun
  j     Day of the month without leading zeros                                    1 to 31
  l     A full textual representation of the day of the week                      Sunday to Saturday
  N     ISO-8601 numeric representation of the day of the week                    1 (for Monday) through 7 (for Sunday)
  S     English ordinal suffix for the day of the month, 2 characters             st, nd, rd or th. Works well with j
  w     Numeric representation of the day of the week                             0 (for Sunday) to 6 (for Saturday)
  z     The day of the year (starting from 0)                                     0 to 364 (365 in leap years)
  W     ISO-8601 week number of year, weeks starting on Monday                    01 to 53
  F     A full textual representation of a month, such as January or March        January to December
  m     Numeric representation of a month, with leading zeros                     01 to 12
  M     A short textual representation of a month                                 Jan to Dec
  n     Numeric representation of a month, without leading zeros                  1 to 12
  t     Number of days in the given month                                         28 to 31
  L     Whether it's a leap year                                                  1 if it is a leap year, 0 otherwise.
  o     ISO-8601 year number (identical to (Y), but if the ISO week number (W)    Examples: 1998 or 2004
        belongs to the previous or next year, that year is used instead)
  Y     A full numeric representation of a year, 4 digits                         Examples: 1999 or 2003
  y     A two digit representation of a year                                      Examples: 99 or 03
  a     Lowercase Ante meridiem and Post meridiem                                 am or pm
  A     Uppercase Ante meridiem and Post meridiem                                 AM or PM
  g     12-hour format of an hour without leading zeros                           1 to 12
  G     24-hour format of an hour without leading zeros                           0 to 23
  h     12-hour format of an hour with leading zeros                              01 to 12
  H     24-hour format of an hour with leading zeros                              00 to 23
  i     Minutes, with leading zeros                                               00 to 59
  s     Seconds, with leading zeros                                               00 to 59

Comments

  1. when i am trying to parse "Sun Nov 19 00:00:00 IST 2017" getting null value

    ReplyDelete
    Replies
    1. i am getting same error , i just want to display only 1- 12 hours

      Delete
  2. It was really a nice post and I was really impressed by reading this keep updating Angular Training
    React js Online Training
    NodeJS Course

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks for sharing this information, this is useful to me...
    Services and package prices may vary depends on your business needs and volume of transactions.
    QuickBooks ProAdvisor
    payroll processing


    ReplyDelete
  5. Nice Article. Thanks for sharing
    Contact us:
    Locksmith Melbourne,
    Melbourne, VIC-3000,
    Australia.
    Phone: 03 9464 1230
    eMail: info@locksmithmelbourne.net
    Web: https://locksmithmelbourne.net/

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This post is so helpfull and informative.keep updating with more information...
    Angular JS Framework
    AngularJS Concepts

    ReplyDelete
  8. Nciecacont-pu Irla Burns Here
    boatratealin

    ReplyDelete

Post a Comment

Popular posts from this blog

Ext4 Apply Store Filtering

In extjs4.1: There are many way for store filtering . Some of code i give here Filtering by single field: store . filter ( 'eyeColor' , 'Brown' );   Alternatively, if filters are configured with an  id , then existing filters store may be  replaced by new filters having the same  id . Filtering by single field: store . filter ( "email" , /\.com$/ );   Using multiple filters: store . filter ([ { property : "email" , value : /\.com$/ }, { filterFn : function ( item ) { return item . get ( "age" ) > 10 ; }} ]);   Using  Ext.util.Filter  instances instead of config objects (note that we need to specify the root config option in this case): store . filter ([ Ext . create ( ' Ext.util.Filter ' , {   property : "email" , value : /\.com$/ , root : 'data' }),   Ext . create ( ' Ext.util.Filter ' , {   filterFn : function ( item ) {   return item . get ( &

ExtJS - Grid panel features

What can we do with ExtJS GridPanel? I have to develop a lot of applications in my web app and I see that grid component of ExtJS may fit in. However, I am not aware of what all things I can do with the - off the shelf available framework pieces - available plug-ins in the marketplace and - custom development through my own developers This is a typical question that we hear from the business users who wants to design an application by keeping the framework’s capability in perspective. In this article I have tried to put the list of stuff you can do with grid and hopefully that shall enable you to take advantage of the beauty of ExtJS. Pre-requisites This article assumes that you are familiar with basics of ExtJS What are the available options? In this section I will be taking you through some of the commonly seen usage of ExtJS grid panel. While covering all the capabilities of grid may not be possible, I am sure it will be helpful for the business users who want to

EXTJS - Way to get a reference to the viewport from any controller

An easy way to get a reference to the viewport from any controller Here's an easy way to get a reference to your viewport from any controller: Ext.application({ launch: function(){ this.viewport = Ext.ComponentQuery.query('viewport')[0]; this.centerRegion = this.viewport.down('[region=center]'); } }); then, inside say, a controller, you can call this: this.application.viewport to get a reference to the viewport.