Print Friendly

Class Date

Package:Global
Class:Date
Extends:Object
Defined In:Date.js
The date parsing and format syntax is a subset of PHP's date() function, and the formats that are supported will provide results equivalent to their PHP versions. Following is the list of all currently supported formats:
Sample date:
'Wed Jan 10 2007 15:05:01 GMT-0600 (Central Standard Time)'

Format  Output      Description
------  ----------  --------------------------------------------------------------
  d      10         Day of the month, 2 digits with leading zeros
  D      Wed        A textual representation of a day, three letters
  j      10         Day of the month without leading zeros
  l      Wednesday  A full textual representation of the day of the week
  S      th         English ordinal day of month suffix, 2 chars (use with j)
  w      3          Numeric representation of the day of the week
  z      9          The julian date, or day of the year (0-365)
  W      01         ISO-8601 2-digit week number of year, weeks starting on Monday (00-52)
  F      January    A full textual representation of the month
  m      01         Numeric representation of a month, with leading zeros
  M      Jan        Month name abbreviation, three letters
  n      1          Numeric representation of a month, without leading zeros
  t      31         Number of days in the given month
  L      0          Whether it's a leap year (1 if it is a leap year, else 0)
  Y      2007       A full numeric representation of a year, 4 digits
  y      07         A two digit representation of a year
  a      pm         Lowercase Ante meridiem and Post meridiem
  A      PM         Uppercase Ante meridiem and Post meridiem
  g      3          12-hour format of an hour without leading zeros
  G      15         24-hour format of an hour without leading zeros
  h      03         12-hour format of an hour with leading zeros
  H      15         24-hour format of an hour with leading zeros
  i      05         Minutes with leading zeros
  s      01         Seconds, with leading zeros
  O      -0600      Difference to Greenwich time (GMT) in hours
  T      CST        Timezone setting of the machine running the code
  Z      -21600     Timezone offset in seconds (negative if west of UTC, positive if east)
Example usage (note that you must escape format specifiers with '\\' to render them as character literals):
var dt = new Date('1/10/2007 03:05:01 PM GMT-0600');
document.write(dt.format('Y-m-d'));                         //2007-01-10
document.write(dt.format('F j, Y, g:i a'));                 //January 10, 2007, 3:05 pm
document.write(dt.format('l, \\t\\he dS of F Y h:i:s A'));  //Wednesday, the 10th of January 2007 03:05:01 PM
Here are some standard date/time patterns that you might find helpful. They are not part of the source of Date.js, but to use them you can simply copy this block of code into any script that is included after Date.js and they will also become globally available on the Date object. Feel free to add or remove patterns as needed in your code.
Date.patterns = {
    ISO8601Long:"Y-m-d H:i:s",
    ISO8601Short:"Y-m-d",
    ShortDate: "n/j/Y",
    LongDate: "l, F d, Y",
    FullDateTime: "l, F d, Y g:i:s A",
    MonthDay: "F d",
    ShortTime: "g:i A",
    LongTime: "g:i:s A",
    SortableDateTime: "Y-m-d\\TH:i:s",
    UniversalSortableDateTime: "Y-m-d H:i:sO",
    YearMonth: "F, Y"
};
Example usage:
var dt = new Date();
document.write(dt.format(Date.patterns.ShortDate));

Properties   -  Methods   -  Events

Public Properties

Property Defined By
  Date.DAY : String Date
<static> Date interval constant
  Date.HOUR : String Date
<static> Date interval constant
  Date.MILLI : String Date
<static> Date interval constant
  Date.MINUTE : String Date
<static> Date interval constant
  Date.MONTH : String Date
<static> Date interval constant
  Date.SECOND : String Date
<static> Date interval constant
  Date.YEAR : String Date
<static> Date interval constant
  Date.dayNames : Array Date
<static> An array of textual day names. Override these values for international dates, for example... Date.dayN...
  Date.monthNames : Array Date
<static> An array of textual month names. Override these values for international dates, for example... Date.mo...

Public Methods

Method Defined By
  Date.parseDateString input, String format ) : Date Date
<static> Parses the passed string using the specified format. Note that this function expects dates in normal c...
  addString interval, Number value ) : Date Date
Provides a convenient method of performing basic date arithmetic. This method does not modify the Date instance bein...
  clearTimeBoolean clone ) : Date Date
Clears any time information from this date
  clone() : Date Date
Creates and returns a new Date instance with the exact same date value as the called instance. Dates are copied and p...
  formatString format ) : String Date
Formats a date given the supplied format string
  getDayOfYear() : Number Date
Get the numeric day number of the year, adjusted for leap year.
  getDaysInMonth() : Number Date
Get the number of days in the current month, adjusted for leap year.
  getElapsed[Date date] ) : Number Date
Returns the number of milliseconds between this date and date
  getFirstDateOfMonth() : Date Date
Get a Date of the first day of this date's month
  getFirstDayOfMonth() : Number Date
Get the first day of the current month, adjusted for leap year. The returned value is the numeric day index within t...
  getGMTOffset() : String Date
Get the offset from GMT of the current date (equivalent to the format specifier 'O').
  getLastDateOfMonth() : Date Date
Get a Date of the late day of this date's month
  getLastDayOfMonth() : Number Date
Get the last day of the current month, adjusted for leap year. The returned value is the numeric day index within th...
  getSuffix() : String Date
Get the English ordinal suffix of the current day (equivalent to the format specifier 'S').
  getTimezone() : String Date
Get the timezone abbreviation of the current date (equivalent to the format specifier 'T').
  getWeekOfYear() : String Date
Get the string representation of the numeric week number of the year (equivalent to the format specifier 'W').
  isLeapYear() : Boolean Date
Whether or not the current date is in a leap year.

Public Events

This class has no public events.

Property Details

Date.DAY

public String Date.DAY
<static> Date interval constant
This property is defined by Date.

Date.HOUR

public String Date.HOUR
<static> Date interval constant
This property is defined by Date.

Date.MILLI

public String Date.MILLI
<static> Date interval constant
This property is defined by Date.

Date.MINUTE

public String Date.MINUTE
<static> Date interval constant
This property is defined by Date.

Date.MONTH

public String Date.MONTH
<static> Date interval constant
This property is defined by Date.

Date.SECOND

public String Date.SECOND
<static> Date interval constant
This property is defined by Date.

Date.YEAR

public String Date.YEAR
<static> Date interval constant
This property is defined by Date.

Date.dayNames

public Array Date.dayNames
<static> An array of textual day names. Override these values for international dates, for example... Date.dayNames = ['SundayInYourLang', 'MondayInYourLang', ...];
This property is defined by Date.

Date.monthNames

public Array Date.monthNames
<static> An array of textual month names. Override these values for international dates, for example... Date.monthNames = ['JanInYourLang', 'FebInYourLang', ...];
This property is defined by Date.

Method Details

Date.parseDate

public function Date.parseDate( String input, String format )
<static> Parses the passed string using the specified format. Note that this function expects dates in normal calendar format, meaning that months are 1-based (1 = January) and not zero-based like in JavaScript dates. Any part of the date format that is not specified will default to the current date value for that part. Time parts can also be specified, but default to 0. Keep in mind that the input date string must precisely match the specified format string or the parse operation will fail. Example Usage:
//dt = Fri May 25 2007 (current date)
var dt = new Date();

//dt = Thu May 25 2006 (today's month/day in 2006)
dt = Date.parseDate("2006", "Y");

//dt = Sun Jan 15 2006 (all date parts specified)
dt = Date.parseDate("2006-1-15", "Y-m-d");

//dt = Sun Jan 15 2006 15:20:01 GMT-0600 (CST)
dt = Date.parseDate("2006-1-15 3:20:01 PM", "Y-m-d h:i:s A" );
Parameters:
  • input : String
    The unparsed date as a string
  • format : String
    The format the date is in
Returns:
  • Date
    The parsed date
This method is defined by Date.

add

public function add( String interval, Number value )
Provides a convenient method of performing basic date arithmetic. This method does not modify the Date instance being called - it creates and returns a new Date instance containing the resulting date value. Examples:
//Basic usage:
var dt = new Date('10/29/2006').add(Date.DAY, 5);
document.write(dt); //returns 'Fri Oct 06 2006 00:00:00'

//Negative values will subtract correctly:
var dt2 = new Date('10/1/2006').add(Date.DAY, -5);
document.write(dt2); //returns 'Tue Sep 26 2006 00:00:00'

//You can even chain several calls together in one line!
var dt3 = new Date('10/1/2006').add(Date.DAY, 5).add(Date.HOUR, 8).add(Date.MINUTE, -30);
document.write(dt3); //returns 'Fri Oct 06 2006 07:30:00'
Parameters:
  • interval : String
    A valid date interval enum value
  • value : Number
    The amount to add to the current date
Returns:
  • Date
    The new Date instance
This method is defined by Date.

clearTime

public function clearTime( Boolean clone )
Clears any time information from this date
Parameters:
  • clone : Boolean
    true to create a clone of this date, clear the time and return it
Returns:
  • Date
    this or the clone
This method is defined by Date.

clone

public function clone()
Creates and returns a new Date instance with the exact same date value as the called instance. Dates are copied and passed by reference, so if a copied date variable is modified later, the original variable will also be changed. When the intention is to create a new variable that will not modify the original instance, you should create a clone. Example of correctly cloning a date:
//wrong way:
var orig = new Date('10/1/2006');
var copy = orig;
copy.setDate(5);
document.write(orig);  //returns 'Thu Oct 05 2006'!

//correct way:
var orig = new Date('10/1/2006');
var copy = orig.clone();
copy.setDate(5);
document.write(orig);  //returns 'Thu Oct 01 2006'
Parameters:
  • None.
Returns:
  • Date
    The new Date instance
This method is defined by Date.

format

public function format( String format )
Formats a date given the supplied format string
Parameters:
  • format : String
    The format string
Returns:
  • String
    The formatted date
This method is defined by Date.

getDayOfYear

public function getDayOfYear()
Get the numeric day number of the year, adjusted for leap year.
Parameters:
  • None.
Returns:
  • Number
    0 through 364 (365 in leap years)
This method is defined by Date.

getDaysInMonth

public function getDaysInMonth()
Get the number of days in the current month, adjusted for leap year.
Parameters:
  • None.
Returns:
  • Number
    The number of days in the month
This method is defined by Date.

getElapsed

public function getElapsed( [Date date] )
Returns the number of milliseconds between this date and date
Parameters:
  • date : Date
    (optional) Defaults to now
Returns:
  • Number
    The diff in milliseconds
This method is defined by Date.

getFirstDateOfMonth

public function getFirstDateOfMonth()
Get a Date of the first day of this date's month
Parameters:
  • None.
Returns:
  • Date
This method is defined by Date.

getFirstDayOfMonth

public function getFirstDayOfMonth()
Get the first day of the current month, adjusted for leap year. The returned value is the numeric day index within the week (0-6) which can be used in conjunction with the monthNames array to retrieve the textual day name. Example:
var dt = new Date('1/10/2007');
document.write(Date.dayNames[dt.getFirstDayOfMonth()]); //output: 'Monday'
Parameters:
  • None.
Returns:
  • Number
    The day number (0-6)
This method is defined by Date.

getGMTOffset

public function getGMTOffset()
Get the offset from GMT of the current date (equivalent to the format specifier 'O').
Parameters:
  • None.
Returns:
  • String
    The 4-character offset string prefixed with + or - (e.g. '-0600')
This method is defined by Date.

getLastDateOfMonth

public function getLastDateOfMonth()
Get a Date of the late day of this date's month
Parameters:
  • None.
Returns:
  • Date
This method is defined by Date.

getLastDayOfMonth

public function getLastDayOfMonth()
Get the last day of the current month, adjusted for leap year. The returned value is the numeric day index within the week (0-6) which can be used in conjunction with the monthNames array to retrieve the textual day name. Example:
var dt = new Date('1/10/2007');
document.write(Date.dayNames[dt.getLastDayOfMonth()]); //output: 'Wednesday'
Parameters:
  • None.
Returns:
  • Number
    The day number (0-6)
This method is defined by Date.

getSuffix

public function getSuffix()
Get the English ordinal suffix of the current day (equivalent to the format specifier 'S').
Parameters:
  • None.
Returns:
  • String
    'st, 'nd', 'rd' or 'th'
This method is defined by Date.

getTimezone

public function getTimezone()
Get the timezone abbreviation of the current date (equivalent to the format specifier 'T').
Parameters:
  • None.
Returns:
  • String
    The abbreviated timezone name (e.g. 'CST')
This method is defined by Date.

getWeekOfYear

public function getWeekOfYear()
Get the string representation of the numeric week number of the year (equivalent to the format specifier 'W').
Parameters:
  • None.
Returns:
  • String
    '00' through '52'
This method is defined by Date.

isLeapYear

public function isLeapYear()
Whether or not the current date is in a leap year.
Parameters:
  • None.
Returns:
  • Boolean
    True if the current date is in a leap year, else false
This method is defined by Date.

Ext - Copyright © 2006-2007 Ext JS, LLC
All rights reserved.