datatype-date.js revision aad83354a51620a64a2453eaf7b0e8460523a137
1008N/A * The DataType utility provides a set of utility functions to operate on native 1008N/A * @submodule datatype-number-format 1008N/A * Converts data to type Date. 2362N/A * @param data {String | Number} Data to convert. Values supported by the Date constructor are supported. 1008N/A * @return {Date} A Date, or null. 1821N/A * The Date formatter utility implements strftime formatters for javascript based on the 1821N/A * Open Group specification defined at 1008N/A * This implementation does not include modified conversion specifiers (i.e., Ex and Ox) 1008N/A * The following format specifiers are supported: 1821N/A * This library may be used as follows: 1821N/A * var d = Y.DataType.Date.format(new Date("2009/04/10", { format: "Today is %A, the %d of %B, %Y" }); 2228N/A * @title DataType Date Formatter Submodule 0N/A * Pad a number with leading spaces, zeroes or something else 2402N/A * @param x {Number} The number to be padded 2402N/A * @param pad {String} The character to pad the number with 2402N/A * @param r {Number} (optional) The base of the pad, eg, 10 implies to two digits, 100 implies to 3 digits. 2402N/A if(
typeof r ===
"undefined")
1821N/A * Date subclass for the YUI DataType utility. 2402N/A }
else if(W===
0 && V>=
52) {
2228N/A // First week is 01 and not 00 as in the case of %U and %W, 1821N/A // so we add 1 to the final result except if day 1 of the year 2228N/A // is a Monday (then %W returns 01). 2228N/A // We also need to subtract 1 if the day 1 of the year is 1821N/A // Friday-Sunday, so the resulting equation becomes: 2228N/A return (o>
0?
"-":
"+") + H + M;
1008N/A "%":
function (d) {
return "%"; }
1008N/A * Takes a native JavaScript Date and formats it as a string for display to user. 1008N/A * @param oDate {Date} Date. 1008N/A * @param oConfig {Object} (Optional) Object literal of configuration values: 1821N/A * <dt>format {String} (Optional)</dt> 0N/A * Any strftime string is supported, such as "%I:%M:%S %p". strftime has several format specifiers defined by the Open group at 1008N/A * This javascript implementation supports all the PHP specifiers and a few more. The full list is below. 1821N/A * If not specified, it defaults to the ISO8601 standard date format: %Y-%m-%d. This may be overridden by changing Y.config.dateFormat 1821N/A * <dt>%a</dt> <dd>abbreviated weekday name according to the current locale</dd> 1821N/A * <dt>%A</dt> <dd>full weekday name according to the current locale</dd> 1821N/A * <dt>%b</dt> <dd>abbreviated month name according to the current locale</dd> 1821N/A * <dt>%B</dt> <dd>full month name according to the current locale</dd> 1821N/A * <dt>%c</dt> <dd>preferred date and time representation for the current locale</dd> 1821N/A * <dt>%C</dt> <dd>century number (the year divided by 100 and truncated to an integer, range 00 to 99)</dd> 1821N/A * <dt>%d</dt> <dd>day of the month as a decimal number (range 01 to 31)</dd> 1821N/A * <dt>%D</dt> <dd>same as %m/%d/%y</dd> 1821N/A * <dt>%e</dt> <dd>day of the month as a decimal number, a single digit is preceded by a space (range " 1" to "31")</dd> 1821N/A * <dt>%F</dt> <dd>same as %Y-%m-%d (ISO 8601 date format)</dd> 1821N/A * <dt>%g</dt> <dd>like %G, but without the century</dd> 1821N/A * <dt>%G</dt> <dd>The 4-digit year corresponding to the ISO week number</dd> 2402N/A * <dt>%h</dt> <dd>same as %b</dd> 2402N/A * <dt>%H</dt> <dd>hour as a decimal number using a 24-hour clock (range 00 to 23)</dd> 2402N/A * <dt>%I</dt> <dd>hour as a decimal number using a 12-hour clock (range 01 to 12)</dd> 2391N/A * <dt>%j</dt> <dd>day of the year as a decimal number (range 001 to 366)</dd> 2391N/A * <dt>%k</dt> <dd>hour as a decimal number using a 24-hour clock (range 0 to 23); single digits are preceded by a blank. (See also %H.)</dd> 1008N/A * <dt>%l</dt> <dd>hour as a decimal number using a 12-hour clock (range 1 to 12); single digits are preceded by a blank. (See also %I.) </dd> 1008N/A * <dt>%m</dt> <dd>month as a decimal number (range 01 to 12)</dd> 1008N/A * <dt>%M</dt> <dd>minute as a decimal number</dd> 1008N/A * <dt>%n</dt> <dd>newline character</dd> 1008N/A * <dt>%p</dt> <dd>either "AM" or "PM" according to the given time value, or the corresponding strings for the current locale</dd> 0N/A * <dt>%P</dt> <dd>like %p, but lower case</dd> 1008N/A * <dt>%r</dt> <dd>time in a.m. and p.m. notation equal to %I:%M:%S %p</dd> 1008N/A * <dt>%R</dt> <dd>time in 24 hour notation equal to %H:%M</dd> 2391N/A * <dt>%s</dt> <dd>number of seconds since the Epoch, ie, since 1970-01-01 00:00:00 UTC</dd> 1008N/A * <dt>%S</dt> <dd>second as a decimal number</dd> 3866N/A * <dt>%t</dt> <dd>tab character</dd> 1008N/A * <dt>%T</dt> <dd>current time, equal to %H:%M:%S</dd> 1821N/A * <dt>%u</dt> <dd>weekday as a decimal number [1,7], with 1 representing Monday</dd> 1821N/A * <dt>%U</dt> <dd>week number of the current year as a decimal number, starting with the 1821N/A * first Sunday as the first day of the first week</dd> 1821N/A * <dt>%V</dt> <dd>The ISO 8601:1988 week number of the current year as a decimal number, 1821N/A * range 01 to 53, where week 1 is the first week that has at least 4 days 1821N/A * in the current year, and with Monday as the first day of the week.</dd> 1947N/A * <dt>%w</dt> <dd>day of the week as a decimal, Sunday being 0</dd> 1947N/A * <dt>%W</dt> <dd>week number of the current year as a decimal number, starting with the 1947N/A * first Monday as the first day of the first week</dd> 1947N/A * <dt>%x</dt> <dd>preferred date representation for the current locale without the time</dd> 1947N/A * <dt>%X</dt> <dd>preferred time representation for the current locale without the date</dd> 1947N/A * <dt>%y</dt> <dd>year as a decimal number without a century (range 00 to 99)</dd> 1947N/A * <dt>%Y</dt> <dd>year as a decimal number including the century</dd> 1947N/A * <dt>%z</dt> <dd>numerical time zone representation</dd> 1947N/A * <dt>%Z</dt> <dd>time zone name or abbreviation</dd> 2017N/A * <dt>%%</dt> <dd>a literal "%" character</dd> 1821N/A * <dt>locale {String} (Optional)</dt> 1821N/A * The locale to use when displaying days of week, months of the year, and other locale specific 1947N/A * strings. If not specified, this defaults to "en" (though this may be overridden by changing Y.config.locale). 1947N/A * The following locales are built in: 1821N/A * <dd>Australian English (identical to British English)</dd> 1947N/A * More locales may be added by subclassing of Y.DataType.Date.Locale["en"]. 1821N/A * See Y.DataType.Date.Locale for more information. 1947N/A * @return {String} Formatted date for display. 1947N/A * @sa Y.DataType.Date.Locale 3866N/A // Make sure we have a definition for the requested locale, or default to en. 1008N/A case "string":
// string => built in date function 1008N/A case "function":
// function => our own function 1934N/A case "array":
// built in function with padding 1934N/A }
// no break; (fall through to default:) 1821N/A // First replace aggregates (run in a loop because an agg may be made up of other aggs) 1821N/A // Now replace formats (do not run in a loop otherwise %%a will be replace with the value of %a) 1821N/A * The Date.Locale class is a container for all localised date strings 1934N/A * used by Y.DataType.Date. It is used internally, but may be extended 1821N/A * to provide new date localisations. 1821N/A * To create your own Locale, follow these steps: 3866N/A * <li>Find an existing locale that matches closely with your needs</li> 1821N/A * <li>Use this as your base class. Use Y.DataType.Date.Locale["en"] if nothing 3866N/A * <li>Create your own class as an extension of the base class using 1821N/A * Y.merge, and add your own localisations where needed.</li> 2500N/A * See the Y.DataType.Date.Locale["en-US"] and Y.DataType.Date.Locale["en-GB"] 2228N/A * classes which extend Y.DataType.Date.Locale["en"]. 1934N/A * For example, to implement locales for French french and Canadian french, 2228N/A * we would do the following: 1934N/A * <li>For French french, we have no existing similar locale, so use 1821N/A * Y.DataType.Date.Locale["en"] as the base, and extend it: 3866N/A * Y.DataType.Date.Locale["fr"] = Y.merge(Y.DataType.Date.Locale, { 1821N/A * a: ["dim", "lun", "mar", "mer", "jeu", "ven", "sam"], 1821N/A * A: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"], 1821N/A * b: ["jan", "fév", "mar", "avr", "mai", "jun", "jui", "aoû", "sep", "oct", "nov", "déc"], 1821N/A * B: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"], 1821N/A * <li>For Canadian french, we start with French french and change the meaning of \%x: 3866N/A * Y.DataType.Date.Locale["fr-CA"] = Y.merge(Y.DataType.Date.Locale["fr"], { 2017N/A * With that, you can use your new locales: 2017N/A * var d = new Date("2008/04/22"); 1821N/A * Y.DataType.Date.format(d, { format: "%A, %d %B == %x", locale: "fr" }); 1821N/A * mardi, 22 avril == 22.04.2008 1821N/A * Y.DataType.Date.format(d, {format: "%A, %d %B == %x", locale: "fr-CA" }); 1821N/A * mardi, 22 avril == 2008-04-22 3740N/A * @class DataType.Date.Locale 1821N/A a: [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"],
1821N/A A: [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"],
1821N/A b: [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"],
1821N/A B: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"],
1934N/A c:
"%a %d %b %Y %I:%M:%S %p %Z",
1821N/AYUI.
add(
'datatype-date',
function(Y){},
'@VERSION@' ,{
use:[
'datatype-date-parse',
'datatype-date-format']});