Mixed into
Files
Extra methods to be mixed into Ext.Date.
Require this class to get Ext.Date with all the methods listed below.
Using Ext.setup:
Ext.setup({
requires: 'Ext.DateExtras',
onReady: function() {
var date = new Date();
alert(Ext.Date.format(date, 'n/j/Y'));
}
});
Using Ext.application:
Ext.application({
requires: 'Ext.DateExtras',
launch: function() {
var date = new Date();
alert(Ext.Date.format(date, 'n/j/Y'));
}
});
An array of textual day names. Override these values for international dates. Example:
Ext.Date.dayNames = [
'SundayInYourLang',
'MondayInYourLang'
// ...
];
Defaults to: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
The date format string that the Ext.util.Format.date function uses. See Ext.Date for details.
This defaults to m/d/Y
, but may be overridden in a locale file.
Defaults to: "m/d/Y"
An object hash containing default date values used during date parsing.
The following properties are available:
y
: Number - The default year value. Defaults to undefined
.m
: Number - The default 1-based month value. Defaults to undefined
.d
: Number - The default day value. Defaults to undefined
.h
: Number - The default hour value. Defaults to undefined
.i
: Number - The default minute value. Defaults to undefined
.s
: Number - The default second value. Defaults to undefined
.ms
: Number - The default millisecond value. Defaults to undefined
.Override these properties to customize the default date values used by the parse method.
Note: In countries which experience Daylight Saving Time (i.e. DST), the h
, i
, s
and ms
properties may coincide with the exact time in which DST takes effect.
It is the responsibility of the developer to account for this.
Example Usage:
// set default day value to the first day of the month
Ext.Date.defaults.d = 1;
// parse a February date string containing only year and month values.
// setting the default day value to 1 prevents weird date rollover issues.
// when attempting to parse the following date string on, for example, March 31st 2009.
alert(Ext.Date.parse('2009-02', 'Y-m')); // returns a Date object representing February 1st 2009.
Defaults to: {}
The base format-code to formatting-function hashmap used by the format method. Formatting functions are strings (or functions which return strings) which will return the appropriate value when evaluated in the context of the Date object from which the format method is called. Add to / override these mappings for custom date formatting.
Note: Ext.Date.format()
treats characters as literals if an appropriate mapping cannot be found.
Example:
Ext.Date.formatCodes.x = "Ext.util.Format.leftPad(this.getDate(), 2, '0')";
alert(Ext.Date.format(new Date(), 'x')); // returns the current day of the month
An object hash in which each property is a date formatting function. The property name is the format string which corresponds to the produced formatted date string.
This object is automatically populated with date formatting functions as date formats are requested for Ext standard formatting strings.
Custom formatting functions may be inserted into this object, keyed by a name which from then on may be used as a format string to format.
Example:
Ext.Date.formatFunctions['x-date-format'] = myDateFormatter;
A formatting function should return a string representation of the Date object which is the scope (this) of the function.
To enable date strings to also be parsed according to that format, a corresponding parsing function must be placed into the parseFunctions property.
An array of textual month names. Override these values for international dates. Example:
Ext.Date.monthNames = [
'JanInYourLang',
'FebInYourLang'
// ...
];
Defaults to: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
An object hash of zero-based JavaScript month numbers (with short month names as keys).
Note: keys are case-sensitive.
Override these values for international dates. Example:
Ext.Date.monthNumbers = {
'ShortJanNameInYourLang': 0,
'ShortFebNameInYourLang': 1
// ...
};
Defaults to: {Jan: 0, Feb: 1, Mar: 2, Apr: 3, May: 4, Jun: 5, Jul: 6, Aug: 7, Sep: 8, Oct: 9, Nov: 10, Dec: 11}
An object hash in which each property is a date parsing function. The property name is the format string which that function parses.
This object is automatically populated with date parsing functions as date formats are requested for Ext standard formatting strings.
Custom parsing functions may be inserted into this object, keyed by a name which from then on may be used as a format string to parse.
Example:
Ext.Date.parseFunctions['x-date-format'] = myDateParser;
A parsing function should return a Date object, and is passed the following parameters:
date
: String - The date string to parse.strict
: Boolean - true
to validate date strings while parsing
(i.e. prevent JavaScript Date "rollover"). The default must be false
.
Invalid date strings should return null
when parsed.To enable Dates to also be formatted according to that format, a corresponding formatting function must be placed into the formatFunctions property.
Global flag which determines if strict date parsing should be used. Strict date parsing will not roll-over invalid dates, which is the default behavior of JavaScript Date objects. (see parse for more information)
Defaults to: false
Provides a convenient method for 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.
// Basic usage:
var dt = Ext.Date.add(new Date('10/29/2006'), Ext.Date.DAY, 5);
alert(dt); // 'Fri Nov 03 2006 00:00:00'
You can also subtract date values by passing a negative value:
// Negative values will be subtracted:
var dt2 = Ext.Date.add(new Date('10/1/2006'), Ext.Date.DAY, -5);
alert(dt2); // 'Tue Sep 26 2006 00:00:00'
The date to modify.
A valid date interval enum value.
The amount to add to the current date.
The new Date instance.
Attempts to clear all time information from this Date by setting the time to midnight of the same day, automatically adjusting for Daylight Saving Time (DST) where applicable.
Note: DST timezone information for the browser's host operating system is assumed to be up-to-date.
The date.
true
to create a clone of this date, clear the time and return it.
Defaults to: false
this or the 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);
console.log(orig); // returns 'Thu Oct 05 2006'!
// correct way:
var orig = new Date('10/1/2006'),
copy = Ext.Date.clone(orig);
copy.setDate(5);
console.log(orig); // returns 'Thu Oct 01 2006'
The date.
The new Date instance.
Get the numeric day number of the year, adjusted for leap year.
var dt = new Date('9/17/2011');
alert(Ext.Date.getDayOfYear(dt)); // 259
The date.
0 to 364 (365 in leap years).
Get the number of days in the current month, adjusted for leap year.
var dt = new Date('1/10/2007');
alert(Ext.Date.getDaysInMonth(dt)); // 31
The date.
The number of days in the month.
Get the date of the first day of the month in which this date resides.
var dt = new Date('1/10/2007'),
lastDate = Ext.Date.getFirstDateOfMonth(dt);
alert(lastDate); // Mon Jan 01 2007 00:00:00 GMT-0800 (PST)
The date.
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.
var dt = new Date('1/10/2007'),
firstDay = Ext.Date.getFirstDayOfMonth(dt);
alert(Ext.Date.dayNames[firstDay]); // 'Monday'
The date
The day number (0-6).
Get the offset from GMT of the current date (equivalent to the format specifier 'O').
var dt = new Date('9/17/2011');
alert(Ext.Date.getGMTOffset(dt));
The date.
true
to separate the hours and minutes with a colon.
Defaults to: false
The 4-character offset string prefixed with + or - (e.g. '-0600').
Get the date of the last day of the month in which this date resides.
var dt = new Date('1/10/2007'),
lastDate = Ext.Date.getLastDateOfMonth(dt);
alert(lastDate); // Wed Jan 31 2007 00:00:00 GMT-0800 (PST)
The date.
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.
var dt = new Date('1/10/2007'),
lastDay = Ext.Date.getLastDayOfMonth(dt);
alert(Ext.Date.dayNames[lastDay]); // 'Wednesday'
The date.
The day number (0-6).
Get the English ordinal suffix of the current day (equivalent to the format specifier 'S').
var dt = new Date('9/17/2011');
alert(Ext.Date.getSuffix(dt)); // 'th'
The date.
'st', 'nd', 'rd' or 'th'.
Get the timezone abbreviation of the current date (equivalent to the format specifier 'T').
Note: The date string returned by the JavaScript Date object's toString()
method varies
between browsers (e.g. FF vs IE) and system region settings (e.g. IE in Asia vs IE in America).
For a given date string e.g. "Thu Oct 25 2007 22:55:35 GMT+0800 (Malay Peninsula Standard Time)",
getTimezone()
first tries to get the timezone abbreviation from between a pair of parentheses
(which may or may not be present), failing which it proceeds to get the timezone abbreviation
from the GMT offset portion of the date string.
var dt = new Date('9/17/2011');
alert(Ext.Date.getTimezone(dt));
The date.
The abbreviated timezone name (e.g. 'CST', 'PDT', 'EDT', 'MPST' ...).
Get the numeric ISO-8601 week number of the year (equivalent to the format specifier 'W', but without a leading zero).
var dt = new Date('9/17/2011');
alert(Ext.Date.getWeekOfYear(dt)); // 37
The date.
1 to 53.
Checks if the current date is affected by Daylight Saving Time (DST).
var dt = new Date('9/17/2011');
alert(Ext.Date.isDST(dt));
The date.
true
if the current date is affected by DST.
Checks if the current date falls within a leap year.
var dt = new Date('1/10/2011');
alert(Ext.Date.isLeapYear(dt)); // false
The date.
true
if the current date falls within a leap year, false
otherwise.
Checks if the passed Date parameters will cause a JavaScript Date "rollover".
4-digit year.
1-based month-of-year.
Day of month.
Hour.
Minute.
Second.
Millisecond.
true
if the passed parameters do not cause a Date "rollover", false
otherwise.
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).
The defaults hash will be used for any date value (i.e. year, month, day, hour, minute, second or millisecond)
which cannot be found in the passed string. If a corresponding default date value has not been specified in the defaults hash,
the current date's year, month, day or DST-adjusted zero-hour time value will be used instead.
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).
Example:
// dt = Fri May 25 2007 (current date)
var dt = new Date();
// dt = Thu May 25 2006 (today's month/day in 2006)
dt = Ext.Date.parse("2006", "Y");
// dt = Sun Jan 15 2006 (all date parts specified)
dt = Ext.Date.parse("2006-01-15", "Y-m-d");
// dt = Sun Jan 15 2006 15:20:01
dt = Ext.Date.parse("2006-01-15 3:20:01 PM", "Y-m-d g:i:s A");
// attempt to parse Sun Feb 29 2006 03:20:01 in strict mode
dt = Ext.Date.parse("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // null
The raw date string.
The expected date string format.
true
to validate date strings while parsing (i.e. prevents JavaScript Date "rollover").
Invalid date strings will return null
when parsed.
Defaults to: false
The parsed Date, or null
if an invalid date string.