date-ext.js | |
---|---|
date-ext 1.0.2 | (function() { |
Private constants | |
Textual representations of days of the week. | var DAYS = [
/* Short */
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
/* Full */
, 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'
, 'Saturday'
] |
Format string for ISO 8601. | , F_ISO_8601 = 'Y-m-d\\TH:i:sP' |
Format string for RFC 2822. | , F_RFC_2822 = 'D, j M Y H:i:s O' |
Textual representations of months. | , MONTHS = [
/* Short */
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'
, 'Nov', 'Dec'
/* Full */
, 'January', 'February', 'March', 'April', 'May', 'June', 'July'
, 'August', 'September', 'October', 'November', 'December'
] |
List of ordinals for S. | , ORDINALS = ['th', 'st', 'nd', 'rd'] |
Regular expression used to tokenise format strings. | , R_TOKEN = /\\?[\\dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU]/g |
Regular expression used to extract timezone identifiers. | , R_TIMEZONE = /([A-Z]+)(?=[\-\+]\d{4})/g; |
Private variables | |
List of every | var tasks = []; |
Private functions | |
Add/subtract the | function addToField(date, field, value) {
if (value) date['set' + field](date['get' + field]() + value);
return date;
} |
Extend the | function extend(name, proto, func) {
if (proto) {
if ('function' !== typeof Date.prototype[name]) {
Date.prototype[name] = func;
}
} else {
if ('function' !== typeof Date[name]) Date[name] = func;
}
} |
Format the given | function format(date, formatStr, params) { |
Use | if ('string' !== typeof formatStr) formatStr = F_RFC_2822; |
Only populate | if (!params) { |
Helper variables to be used when populating parameters. | var iso = getISODate(date)
, j = date.getDate()
, w = date.getDay()
, n = date.getMonth()
, Y = date.getFullYear()
, G = date.getHours()
, e = date.toString().match(R_TIMEZONE); |
Based on the parameters of PHP date. | params = {
/* Day */ |
Day of the month, 2 digits with leading zeros. | d: pad(j) |
Textual representation of a day, three letters. | , D: DAYS[w] |
Day of the month, without leading zeros. | , j: j |
Full textual representation of the day of the week. | , l: DAYS[w + 7] |
ISO 8601 numeric
representation of the day of the week. | , N: w || 7 |
English ordinal suffix for the day of the month, 2 characters. | , S: ORDINALS[(j % 10 > 3) ? 0 : (j % 100 - j % 10 !== 10) * j % 10] |
Numeric representation of the day of the week. | , w: w |
Day of the year. | , z: getDayOfYear(date)
/* Week */ |
ISO 8601 week number of
year, weeks starting on Monday. | , W: iso.week
/* Month */ |
Full textual representation of a month. | , F: MONTHS[n + 12] |
Numeric representation of a month, with leading zeros. | , m: pad(n + 1) |
Textual representation of a month, three letters. | , M: MONTHS[n] |
Numeric representation of a month, without leading zeros. | , n: n + 1 |
Number of days in the given month. | , t: getDaysInMonth(date)
/* Year */ |
Whether it's a leap year. | , L: isLeapYear(date) ? 1 : 0 |
ISO 8601 year number.
This has the same value as Y, except that if the ISO week number
(W) belongs to the previous or next year, that year is used
instead. | , o: iso.year |
Full numeric representation of a year, 4 digits. | , Y: Y |
Numeric representation of a year, 2 digits. | , y: String(Y).slice(2)
/* Time */ |
Lowercase Ante meridiem and Post meridiem. | , a: (G < 12) ? 'am' : 'pm' |
Uppercase Ante meridiem and Post meridiem. | , A: (G < 12) ? 'AM' : 'PM' |
Swatch Internet
Time. | , B: getSwatchInternetTime(date) |
12-hour format of an hour, without leading zeros. | , g: G % 12 || 12 |
24-hour format of an hour, without leading zeros. | , G: G |
12-hour format of an hour, with leading zeros. | , h: pad(G % 12 || 12) |
24-hour format of an hour, with leading zeros. | , H: pad(G) |
Minutes, with leading zeros. | , i: pad(date.getMinutes()) |
Seconds, with leading zeros. | , s: pad(date.getSeconds()) |
Microseconds. | , u: pad(date.getMilliseconds() * 1000, 6)
/* Timezone */ |
Timezone identifier. | , e: e ? e[0] : '' |
Whether or not the date is in daylight saving time. | , I: isDaylightSavingTime(date) ? 1 : 0 |
Difference to Greenwich time (GMT) in hours. | , O: parseTimezoneOffset(date) |
Difference to Greenwich time (GMT), with colon between hours and
minutes. | , P: parseTimezoneOffset(date, ':') |
Timezone abbreviation. | , T: e ? e[0] : '' |
Timezone offset in seconds. The offset for timezones west of UTC
is always negative, and for those east of UTC is always
positive. | , Z: date.getTimezoneOffset() * -60
/* Full Date/Time */ |
ISO 8601 formatted
date. | , c: '' |
RFC 2822 formatted
date. | , r: '' |
Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). | , U: Math.round(date.getTime() / 1000)
}; |
Populate c and r while avoiding infinite circular calls to
| params.c = format(date, F_ISO_8601, params);
params.r = format(date, F_RFC_2822, params);
} |
Replace all parameters within format string while ignoring any escaped by a backslash. | return formatStr.replace(R_TOKEN, function(str) {
if ('\\' === str) return str;
if (0 === str.indexOf('\\')) return str.substr(1);
return params[str];
});
} |
Return the day of the year for | function getDayOfYear(date) {
var start = new Date(date.getFullYear(), 0, 1);
return Math.floor((date - start) / 1000 / 60 / 60 / 24);
} |
Return the days in the month for | function getDaysInMonth(date) {
return (new Date(date.getFullYear(), date.getMonth() + 1, 0)).getDate();
} |
Determine the ISO 8601
information for the given | function getISODate(date) { |
Using UTC is much faster. | var copy = new Date(Date.UTC(date.getFullYear(), date.getMonth()
, date.getDate()))
, day = copy.getUTCDay(); |
Determine the nearest Thursday. | copy.setUTCDate(copy.getUTCDate() - (day + 6) % 7 + 3);
var start = copy.getTime(); |
4th of January is 1st week in ISO 8601. | copy.setUTCMonth(0, 4);
var week = Math.round((start - copy) / (7 * (1000 * 60 * 60 * 24))) + 1;
return {
day: day || 7
, week: week
, year: copy.getUTCFullYear()
};
} |
Calculate the Swatch Internet
Time for the given
| function getSwatchInternetTime(date) {
var time = ((date.getUTCHours() + 1) % 24) + (date.getUTCMinutes() / 60)
+ (date.getUTCSeconds() / (60 * 60));
return Math.floor(time * 1000 / 24);
} |
Return whether or not | function isDaylightSavingTime(date) {
var start = new Date(date);
start.setMonth(0, 1);
return start.getTimezoneOffset() !== date.getTimezoneOffset();
} |
Return whether or not | function isLeapYear(date) {
return 1 === (new Date(date.getFullYear(), 1, 29)).getMonth();
} |
Apply left padding of zero characters to the given | function pad(value, size) {
size = size || 2;
value = String(value);
while (value.length < size) value = '0' + value;
return value;
} |
Parse the timezone offset to appear consistently and with the correct prefix (plus/minus). | function parseTimezoneOffset(date, separator) {
var offset = date.getTimezoneOffset()
, parsed = String(pad(Math.floor(Math.abs(offset) / 60) * 100
+ Math.abs(offset) % 60, 4));
if (separator) parsed = parsed.slice(0, 2) + separator + parsed.slice(2);
return ((offset > 0) ? '-' : '+') + parsed;
} |
Schedule the function provided to be called when | function schedule(date, callback, context) { |
Handle optional arguments accordingly. | if ('string' === typeof callback) {
context = callback;
callback = null;
}
if ('function' === typeof date) {
callback = date;
date = null;
}
if (date == null) date = new Date();
var scheduleId
, time = date - new Date(); |
Simple wrapper function to clear out used tasks before calling the scheduled function. | function wrapper() {
var idx = tasks.indexOf(scheduleId);
if (idx > -1) {
tasks.splice(idx, 1);
callback.call(context);
}
} |
Check that | if ('function' === typeof callback) {
if (time <= 0) {
callback.call(context);
} else {
tasks.push(scheduleId = setTimeout(wrapper, time));
}
}
return scheduleId;
} |
Prevent a scheduled function from being called. | function unschedule(scheduleId) {
var idx = tasks.indexOf(scheduleId);
if (idx > -1) {
clearTimeout(scheduleId);
tasks.splice(idx, 1);
return true;
}
return false;
} |
Public functions | |
Add/subtract the specified number of | extend('addDays', true, function(days) {
return addToField(this, 'Date', days);
}); |
Add/subtract the specified number of | extend('addHours', true, function(hours) {
return addToField(this, 'Hours', hours);
}); |
Add/subtract the specified number of | extend('addMilliseconds', true, function(milliseconds) {
return addToField(this, 'Milliseconds', milliseconds);
}); |
Add/subtract the specified number of | extend('addMinutes', true, function(minutes) {
return addToField(this, 'Minutes', minutes);
}); |
Add/subtract the specified number of | extend('addMonths', true, function(months) {
return addToField(this, 'Month', months);
}); |
Add/subtract the specified number of | extend('addSeconds', true, function(seconds) {
return addToField(this, 'Seconds', seconds);
}); |
Add/subtract the specified number of | extend('addYears', true, function(years) {
return addToField(this, 'FullYear', years);
}); |
Clear the date and time fields for the current date. | extend('clear', true, function() {
this.clearDate();
this.clearTime();
return this;
}); |
Clear the date fields for the current date. | extend('clearDate', true, function() {
this.setFullYear(0, 0, 1);
return this;
}); |
Clear the time fields for the current date. | extend('clearTime', true, function() {
this.setHours(0, 0, 0, 0);
return this;
}); |
Format | extend('format', false, function(date, formatStr) {
return format(date, formatStr);
}); |
Format the current date using the format string provided. | extend('format', true, function(formatStr) {
return format(this, formatStr);
}); |
Return the day of the year for the current date. | extend('getDayOfYear', true, function() {
return getDayOfYear(this);
}); |
Return the days in the month for the current date. | extend('getDaysInMonth', true, function() {
return getDaysInMonth(this);
}); |
Return the ISO 8601 week number of the year for the current date. | extend('getWeekOfYear', true, function() {
return getISODate(this).week;
}); |
Return the ISO 8601 year number.
This has the same value as | extend('getYearOfWeek', true, function() {
return getISODate(this).year;
}); |
Return whether or not the current date is in daylight saving time. | extend('isDaylightSavingTime', true, function() {
return isDaylightSavingTime(this);
}); |
Return whether or not the current date is in a leap year. | extend('isLeapYear', true, function() {
return isLeapYear(this);
}); |
Schedule the function provided to be called when | extend('schedule', false, function(date, callback, context) {
return schedule(date, callback, context);
}); |
Schedule the function provided to be called when the current date is
reached. | extend('schedule', true, function(callback, context) {
return schedule(this, callback, context);
}); |
Prevent a scheduled function from being called. | extend('unschedule', false, unschedule);
extend('unschedule', true, unschedule);
}());
|