Skip to content

Instantly share code, notes, and snippets.

@shergin
Created September 29, 2010 11:02
Show Gist options
  • Save shergin/602556 to your computer and use it in GitHub Desktop.
Save shergin/602556 to your computer and use it in GitHub Desktop.
Date.prototype.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
Date.prototype.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
Date.prototype.military = !/\b(am|pm)\b/i.test(new Date().toLocaleTimeString());
Date.prototype.military = false;
(function () {
var date = new Date();
date.setFullYear(2013, 10, 11);
var string = date.toLocaleDateString();
var notation = '';
if (string.indexOf(11) == 0)
notation = '%d.%m.%Y';
else
if (string.indexOf('11') < string.indexOf('13'))
notation = '%m/%d/%y';
else
notation = '%Y-%m-%d';
Date.prototype.notation = notation;
}());
Date.prototype.parse = function(s) {
with(Date.prototype)
var methods = [
/* 0: nothing */ null,
/* 1: year */ function(x) {this.setFullYear(x < 100 ? (x < 70 ? 2000 + x : 1900 + x) : x);},
/* 2: month */ function(x) {this.setMonth(x - 1);},
/* 3: date */ setDate,
/* 4: hours */ setHours,
/* 5: minutes */ setMinutes,
/* 6: seconds */ setSeconds,
/* 7: am/pm */ function (x) { if (x.toLowerCase() == 'pm') this.setHours(this.getHours() + 12); },
/* 8: 1th */ function (x) { var y = this.getDate(); this.setDate(x); if (y > x) this.setMonth(this.getMonth() + 1); },
/* 9: 2y */ function (x) { this.setFullYear(this.getFullYear() + x); },
/* 10: 3m */ function (x) { this.setMonth(this.getMonth() + x); },
/* 11: 4w */ function (x) { this.setDate(this.getDate() + x * 7); },
/* 12: 5d */ function (x) { this.setDate(this.getDate() + x); }
];
var patterns = [
// 10th 1y 2m 3w 4d
{e: /(-?\d+)\s*\-?\s*th\w*/i, p: [8]},
{e: /(-?\d+)\s*y\w*/i, p: [9]},
{e: /(-?\d+)\s*m\w*/i, p: [10]},
{e: /(-?\d+)\s*w\w*/i, p: [11]},
{e: /(-?\d+)\s*d\w*/i, p: [12]},
// {часы:минуты {am|pm}}
{e: /(^|[^\d])([012]?\d|21|22|23)\s*([\:\-\.]+)\s*([0-5]?\d)\s*(am|pm)?/i, p: [0, 4, 0, 5, 7], time: 1},
// {mm/dd/yy, mm/dd/yyyy} (USA, etc.)
{e: /(^|[^\d])(0?\d|10|11|12)\s*(\/+)\s*([012]?\d|30|31)\s*\3\s*((19|20)?\d\d)([^\d]|$)/, p: [0, 2, 0, 3, 1]},
// {dd.mm.yy, dd.mm.yyyy, /.,-} (EU, Russia, etc.)
{e: /(^|[^\d])([012]?\d|30|31)\s*([\.\-\,\/]+)\s*(0?\d|10|11|12)\s*\3\s*((19|20)?\d\d)([^\d]|$)/, p: [0, 3, 0, 2, 1]},
// {yyyy-mm-dd, yy-mm-dd} (Canada, China, Japan, etc.)
{e: /(^|[^\d])((19|20)?\d\d)\s*([\.\-\/]+)\s*(0?\d|10|11|12)\s*\4\s*([012]?\d|30|31)([^\d]|$)/, p: [0, 1, 0, 0, 2, 3]}
];
var allDay = true;
for (var i = patterns.length; i--;) {
var pattern = patterns[i],
match = s.match(pattern.e);
if (match) {
if (pattern.time)
allDay = false;
var pointers = pattern.p;
for (var j = 0; j < pointers.length; j++)
if (pointers[j] && match[j + 1]) {
var value = parseInt(match[j + 1], 10);
methods[pointers[j]].call(this, isNaN(value) ? match[j + 1] : value);
}
s = s.replace(pattern.e, '');
}
}
this.allDay(allDay);
return this;
}
Date.prototype.format = function(format) {
if (!format)
format = '%c';
function leadingZeros(d) {
return (d < 10 ? '0' : '') + d;
}
// like Python, more: http://docs.python.org/library/time.html#time.strftime
var patterns = {
//'a': // Locale’s abbreviated weekday name.
'A': function() { return this.days[this.getDay()]; }, // Locale’s full weekday name.
//'b': // Locale’s abbreviated month name.
'B': function() { return this.months[this.getMonth()]; }, // Locale’s full month name.
'c': function() { return this.format('%x' + (this.allDay() ? '' : ' %X')); }, // Locale’s appropriate date and time representation.
'd': function() { return leadingZeros(this.getDate()); }, // Day of the month as a decimal number [01,31].
'H': function() { return leadingZeros(this.getHours()); }, // Hour (24-hour clock) as a decimal number [00,23].
'I': function() { return leadingZeros(this.getHours() % 12); }, // Hour (12-hour clock) as a decimal number [01,12].
//'j': // Day of the year as a decimal number [001,366].
'm': function() { return leadingZeros(this.getMonth() + 1); }, // Month as a decimal number [01,12].
'M': function() { return leadingZeros(this.getMinutes()); }, // Minute as a decimal number [00,59].
'p': function() { return this.getHours() < 12 ? 'AM' : 'PM'; }, // Locale’s equivalent of either AM or PM.
'S': function() { return leadingZeros(this.getSeconds()); }, // Second as a decimal number [00,61].
//'U': // Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
//'w': // Weekday as a decimal number [0(Sunday),6].
//'W': // Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
'x': function() { return this.format(this.notation); }, // Locale’s appropriate date representation.
'X': function() { return this.format(this.military ? '%H:%M' : '%I:%M %p'); }, // Locale’s appropriate time representation.
'y': function() { return leadingZeros(this.getFullYear() % 100); }, // Year without century as a decimal number [00,99].
'Y': function() { return '' + this.getFullYear(); }, // Year with century as a decimal number.
//'Z': // Time zone name (no characters if no time zone exists).
};
var context = this,
result = format,
pattern;
function replacer() {
return patterns[pattern].call(context);
}
for (pattern in patterns)
result = result.replace(
'%' + pattern,
replacer
);
return result;
}
Date.prototype.timestamp = function(timestamp) {
if (timestamp)
this.setTime(timestamp * 1000);
return parseInt(this.valueOf() / 1000);
}
Date.prototype.allDay = function(value) {
if (value == undefined)
return this.getSeconds() == 1;
this.setSeconds(value ? 1 : 0);
return this;
}
Date.prototype.clone = function() {
return new Date(this.valueOf());
}
Date.prototype.date = function() {
return new Date(this.getFullYear(), this.getMonth(), this.getDate());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment