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
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:
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 ...
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

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.