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));
|
Date.parseDate( String input , String format ) : Date |
Date |
<static> Parses the passed string using the specified format. Note that this function expects dates in normal c... |
|
add( String interval , Number value ) : Date |
Date |
Provides a convenient method of performing basic date arithmetic. This method
does not modify the Date instance bein... |
|
clearTime( Boolean 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... |
|
format( String 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 the first date 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 the last date 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. |
This class has no public events.
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.
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" );
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:
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
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:
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
: StringThe format string
Returns:
This method is defined by Date.
getDayOfYear
public function getDayOfYear()
Get the numeric day number of the year, adjusted for leap year.
This method is defined by Date.
getDaysInMonth
public function getDaysInMonth()
Get the number of days in the current month, adjusted for leap year.
This method is defined by Date.
getElapsed
public function getElapsed( [Date date
] )
Returns the number of milliseconds between this date and date
Parameters:
Returns:
Number
The diff in milliseconds
This method is defined by Date.
getFirstDateOfMonth
public function getFirstDateOfMonth()
Get the first date of this date's month
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:
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').
This method is defined by Date.
getLastDateOfMonth
public function getLastDateOfMonth()
Get the last date of this date's month
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:
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:
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').
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').
This method is defined by Date.
isLeapYear
public function isLeapYear()
Whether or not the current date is in a leap year.
This method is defined by Date.