From aab79b849f84e7e47e06a15ec2e10f76f3a52d98 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 21 Nov 2010 20:24:02 -0800 Subject: [PATCH] [NEW] DateExt.format (like strftime) --- lib/date-ext.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 lib/date-ext.js diff --git a/lib/date-ext.js b/lib/date-ext.js new file mode 100644 index 0000000..da0a7ec --- /dev/null +++ b/lib/date-ext.js @@ -0,0 +1,98 @@ +exports.extendNative = function() { + Object.keys(DateExt).forEach(function(k) { + if (Date.prototype[k]) return; // don't overwrite existing members + Date.prototype[k] = function() { + var fn = DateExt[k] + , args = [].slice.call(arguments) + ; + args.shift(this); + fn.apply(DateExt, args); + }; + }); +}; + +var Weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', + 'Friday', 'Saturday']; + +var WeekdaysShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; + +var Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', + 'August', 'September', 'October', 'November', 'December']; + +var MonthsShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', + 'Sep', 'Oct', 'Nov', 'Dec']; + +function pad(n, padding) { + padding = padding || '0'; + return n < 10 ? (padding + n) : n; +} + +var DateExt = { + // FIXME write a c extension that uses strftime to do the heavy lifting + format: function(d, fmt) { + return fmt.replace(/%(.)/, function(_, c) { + switch (c) { + case 'A': return Weekdays[d.getDay()]; + case 'a': return WeekdaysShort[d.getDay()]; + case 'B': return Months[d.getMonth()]; + case 'b': // fall through + case 'h': return MonthsShort[d.getMonth()]; + case 'D': return DateExt.format(d, '%m/%d/%y'); + case 'd': return pad(d.getDate()); + case 'e': return d.getDate(); + case 'F': return DateExt.format(d, '%Y-%m-%d'); + case 'H': return pad(d.getHours()); + case 'I': + var hour = d.getHours(); + if (hour == 0) hour = 12; + else if (hour > 12) hour -= 12; + return pad(hour); + case 'k': return pad(d.getHours(), ' '); + case 'l': + var hour = d.getHours(); + if (hour == 0) hour = 12; + else if (hour > 12) hour -= 12; + return pad(hour, ' '); + case 'M': return pad(d.getMinutes()); + case 'm': return pad(d.getMonth() + 1); + case 'n': return '\n'; + case 'p': return d.getHours() < 12 ? 'AM' : 'PM'; + case 'R': return DateExt.format(d, '%H:%M'); + case 'r': return DateExt.format(d, '%I:%M:%S %p'); + case 'S': return pad(d.getSeconds()); + case 's': return d.getTime(); + case 'T': return DateExt.format(d, '%H:%M:%S'); + case 't': return '\t'; + case 'u': + var day = d.getDay(); + return day == 0 ? 7 : day; // 1 - 7, Monday is first day of the week + case 'v': return DateExt.format(d, '%e-%b-%Y'); + case 'w': return d.getDay(); // 0 - 6, Sunday is first day of the week + case 'Y': return d.getFullYear(); + case 'y': + var year = d.getYear(); + return year < 100 ? year : year - 100; + case 'Z': + var tz = d.toString().match(/\((\w+)\)/); + return tz && tz[1] || ''; + case 'z': + var off = d.getTimezoneOffset(); + return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60); + default: return c; + } + }); + }, + month: function(d) { + return Months(d.getMonth()); + }, + shortMonth: function(d) { + return MonthsShort(d.getMonth()); + } + weekday: function(d) { + return Weekdays(d.getDay()); + }, + shortWeekday: function(d) { + return WeekdaysShort(d.getDay()); + } +}; +exports.DateExt = DateExt; \ No newline at end of file