Created
September 7, 2011 01:05
-
-
Save amcjen/1199471 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
// Simulates PHP's date function, ported to lutil from http://jacwright.com/projects/javascript/date_format/ | |
exports.dateFormat = function(date, format) { | |
var returnStr = ''; | |
var replace = { | |
shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | |
longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | |
shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | |
longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | |
// Day | |
d: function() { return (date.getDate() < 10 ? '0' : '') + date.getDate(); }, | |
D: function() { return date.shortDays[date.getDay()]; }, | |
j: function() { return date.getDate(); }, | |
l: function() { return Date.replaceChars.longDays[date.getDay()]; }, | |
N: function() { return date.getDay() + 1; }, | |
S: function() { return (date.getDate() % 10 == 1 && date.getDate() != 11 ? 'st' : (date.getDate() % 10 == 2 && date.getDate() != 12 ? 'nd' : (date.getDate() % 10 == 3 && date.getDate() != 13 ? 'rd' : 'th'))); }, | |
w: function() { return date.getDay(); }, | |
z: function() { var d = new Date(date.getFullYear(),0,1); return Math.ceil((this - d) / 86400000); }, // Fixed now | |
// Week | |
W: function() { var d = new Date(date.getFullYear(), 0, 1); return Math.ceil((((this - d) / 86400000) + d.getDay() + 1) / 7); }, // Fixed now | |
// Month | |
F: function() { return Date.replaceChars.longMonths[date.getMonth()]; }, | |
m: function() { return (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1); }, | |
M: function() { return Date.replaceChars.shortMonths[date.getMonth()]; }, | |
n: function() { return date.getMonth() + 1; }, | |
t: function() { var d = new Date(); return new Date(d.getFullYear(), d.getMonth(), 0).getDate(); }, // Fixed now, gets #days of date | |
// Year | |
L: function() { var year = date.getFullYear(); return (year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)); }, // Fixed now | |
o: function() { var d = new Date(date.valueOf()); d.setDate(d.getDate() - ((date.getDay() + 6) % 7) + 3); return d.getFullYear();}, //Fixed now | |
Y: function() { return date.getFullYear(); }, | |
y: function() { return ('' + date.getFullYear()).substr(2); }, | |
// Time | |
a: function() { return date.getHours() < 12 ? 'am' : 'pm'; }, | |
A: function() { return date.getHours() < 12 ? 'AM' : 'PM'; }, | |
B: function() { return Math.floor((((date.getUTCHours() + 1) % 24) + date.getUTCMinutes() / 60 + date.getUTCSeconds() / 3600) * 1000 / 24); }, // Fixed now | |
g: function() { return date.getHours() % 12 || 12; }, | |
G: function() { return date.getHours(); }, | |
h: function() { return ((date.getHours() % 12 || 12) < 10 ? '0' : '') + (date.getHours() % 12 || 12); }, | |
H: function() { return (date.getHours() < 10 ? '0' : '') + date.getHours(); }, | |
i: function() { return (date.getMinutes() < 10 ? '0' : '') + date.getMinutes(); }, | |
s: function() { return (date.getSeconds() < 10 ? '0' : '') + date.getSeconds(); }, | |
u: function() { var m = date.getMilliseconds(); return (m < 10 ? '00' : (m < 100 ? | |
'0' : '')) + m; }, | |
// Timezone | |
e: function() { return "Not Yet Supported"; }, | |
I: function() { return "Not Yet Supported"; }, | |
O: function() { return (-date.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(date.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(date.getTimezoneOffset() / 60)) + '00'; }, | |
P: function() { return (-date.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(date.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(date.getTimezoneOffset() / 60)) + ':00'; }, // Fixed now | |
T: function() { var m = date.getMonth(); date.setMonth(0); var result = date.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); date.setMonth(m); return result;}, | |
Z: function() { return -date.getTimezoneOffset() * 60; }, | |
// Full Date/Time | |
c: function() { return date.format("Y-m-d\\TH:i:sP"); }, // Fixed now | |
r: function() { return date.toString(); }, | |
U: function() { return date.getTime() / 1000; } | |
}; | |
for (var i = 0; i < format.length; i++) { | |
var curChar = format.charAt(i); | |
if (i - 1 >= 0 && format.charAt(i - 1) == "\\") { | |
returnStr += curChar; | |
} | |
else if (replace[curChar]) { | |
returnStr += replace[curChar].call(this); | |
} else if (curChar != "\\"){ | |
returnStr += curChar; | |
} | |
} | |
return returnStr; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment