Created
September 27, 2011 21:39
-
-
Save joshkehn/1246327 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Date library | |
var log4js = require('log4js'), | |
log = log4js.getLogger('Date'), | |
fmts = { | |
// Returns the full day of the week (e.g. Sunday) | |
'A' : function (d) { | |
var day = d.getUTCDay(); | |
if (day === 0) | |
{ | |
return 'Sunday'; | |
} | |
else if (day === 1) | |
{ | |
return 'Monday'; | |
} | |
else if (day === 2) | |
{ | |
return 'Tuesday'; | |
} | |
else if (day === 3) | |
{ | |
return 'Wednesday'; | |
} | |
else if (day === 4) | |
{ | |
return 'Thursday'; | |
} | |
else if (day === 5) | |
{ | |
return 'Friday'; | |
} | |
else if (day === 6) | |
{ | |
return 'Saturday'; | |
} | |
else | |
{ | |
throw day + " doesn't appear to be a valid day."; | |
} | |
}, | |
// Returns the truncated day of the week (e.g. Sun) | |
'a' : function (d) { | |
return fmts['A'](d).substr(0, 3); | |
}, | |
// Returns day of the month (e.g. 5, 17, 20) | |
'e' : function (d) { | |
return d.getUTCDate(); | |
}, | |
// Returns zero-padded day of the month (e.g. 05, 17 20) | |
'd' : function (d) { | |
var day = fmts['e'](d); | |
if (day < 10) | |
{ | |
return "0" + day; | |
} | |
return day; | |
}, | |
// Not implemented | |
'j' : function (d) { | |
throw "%j not implemented"; | |
}, | |
// Returns the day of the week (0-6) | |
'u' : function (d) { | |
return d.getUTCDate(); | |
}, | |
// Returns the day of the week (0-6) | |
'w' : function (d) { | |
return d.getUTCDate(); | |
}, | |
// Not implemented | |
'U' : function (d) { | |
throw "%U not implemented"; | |
}, | |
// Not implemented | |
'W' : function (d) { | |
throw "%W not implemented"; | |
}, | |
// Not implemented | |
'V' : function (d) { | |
throw "%V not implemented"; | |
}, | |
// Returns the month of the year (e.g. 3, 8, 12) | |
'n' : function (d) { | |
return d.getUTCMonth(); | |
}, | |
// Returns the zero padded month of the yar (e.g. 03, 08, 12) | |
'm' : function (d) { | |
var month = fmts['n'](d); | |
if (month < 10) | |
{ | |
return "0" + month; | |
} | |
return month; | |
}, | |
// Not implemented | |
'h' : function (d) { | |
throw "%h not implemented"; | |
}, | |
// Returns the full month name (e.g. July) | |
'B' : function (d) { | |
var month = fmts['n'](d); | |
if (month === 0) | |
{ | |
return 'January'; | |
} | |
else if (month === 1) | |
{ | |
return 'February'; | |
} | |
else if (month === 2) | |
{ | |
return 'March'; | |
} | |
else if (month === 3) | |
{ | |
return 'April'; | |
} | |
else if (month === 4) | |
{ | |
return 'May'; | |
} | |
else if (month === 5) | |
{ | |
return 'June'; | |
} | |
else if (month === 6) | |
{ | |
return 'July'; | |
} | |
else if (month === 7) | |
{ | |
return 'August'; | |
} | |
else if (month === 8) | |
{ | |
return 'September'; | |
} | |
else if (month === 9) | |
{ | |
return 'October'; | |
} | |
else if (month === 10) | |
{ | |
return 'November'; | |
} | |
else if (month === 11) | |
{ | |
return 'December'; | |
} | |
else | |
{ | |
throw month + " doesn't appear to be a valid month."; | |
} | |
}, | |
// Return shortend month (e.g. Jul) | |
'b' : function (d) { | |
return fmts['B'](d).substr(0, 3); | |
}, | |
// Returns the full year (e.g. 1980) | |
'Y' : function (d) { | |
return d.getUTCFullYear(); | |
}, | |
// Returns the last two digits of a year (e.g. 80) | |
'y' : function (d) { | |
return String(fmts['y'](d)).substr(2, 2); | |
}, | |
// Returns zero-padded minutes | |
'M' : function (d) { | |
var m = d.getUTCMinutes(); | |
if (m < 10) | |
{ | |
m = "0" + m; | |
} | |
return m; | |
}, | |
// Returns the number of seconds since epoch | |
's' : function (d) { | |
return d.getTime() / 1000 | 0; | |
}, | |
// Returns the number of seconds, zero padded | |
'S' : function (d) { | |
var seconds = d.getSeconds(); | |
if (seconds < 10) | |
{ | |
return "0" + seconds; | |
} | |
return seconds; | |
}, | |
// Returns the English ordinal suffix for the day of the month, 2 characters | |
'c' : function (d) { | |
var day = fmts['e'](d); | |
if (day === 1 || day === 21 || day === 31) | |
{ | |
return 'st'; | |
} | |
else if (day === 2 || day === 22) | |
{ | |
return 'nd'; | |
} | |
else if (day === 3 || day === 23) | |
{ | |
return 'rd'; | |
} | |
else if ((day >= 4 && day <= 20) || (day >= 24 && day <= 30)) | |
{ | |
return 'th'; | |
} | |
else | |
{ | |
throw day + " does not appear to be a valid day (1-31)"; | |
} | |
}, | |
// Returns hours in military time | |
'k' : function (d) { | |
return d.getUTCHours(); | |
}, | |
// Returns zero-padded hours in military time | |
'H' : function (d) { | |
var h = fmts['k'](d); | |
if (h < 10) | |
{ | |
h = "0" + h; | |
} | |
return h; | |
}, | |
// Returns timezone | |
'z' : function (d) { | |
var hours = Math.floor(d.getTimezoneOffset() / 60), | |
op = "+"; | |
if (hours > 0) | |
{ | |
op = '-'; | |
} | |
hours = Math.abs(hours); | |
if (hours < 10) | |
{ | |
hours = "0" + hours; | |
} | |
return '-' + hours + '00'; | |
}, | |
'Z' : function (d) { | |
var hours = Math.floor(d.getTimezoneOffset() / 60), | |
op = "+"; | |
if (hours > 0) | |
{ | |
op = '-'; | |
} | |
hours = Math.abs(hours); | |
if (hours < 10) | |
{ | |
hours = "0" + hours; | |
} | |
return op + hours + ':00'; | |
}, | |
'T' : function (d) { | |
return fmts['H'](d) + ':' + fmts['M'](d) + ':' + fmts['S'](d) + ' ' + fmts['z'](d); | |
} | |
}; | |
log.setLevel('FATAL'); | |
module.exports = function (d) { | |
var init, | |
self = this; | |
// Check to see if we were passed a `Date` object. | |
if (d instanceof Date) | |
{ | |
return arguments.callee(d.getTime()); | |
} | |
if (typeof d !== 'undefined') | |
{ | |
init = d; | |
} | |
else | |
{ | |
init = new Date().getTime(); | |
} | |
this.time = function () { | |
return init; | |
}; | |
this.obj = function () { | |
return new Date(init); | |
}; | |
this.format = function (fmt) { | |
// Parse date format | |
var opts = fmt.split('%'), | |
ret = opts[0], | |
l = opts.length; | |
log.trace('Parsing ' + fmt + ' into ' + l + ' parts (' + opts.join('%') + ').'); | |
// Iterate over options. | |
for(var i = 1; i < l; i++) | |
{ | |
log.trace('Formatting ' + opts[i] + ' as ' + opts[i].substr(0, 1)); | |
ret += fmts[opts[i].substr(0,1)](this.obj()) + opts[i].substr(1, opts[i].length); | |
} | |
return ret; | |
}; | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment