Skip to content

Instantly share code, notes, and snippets.

@mattconsto
Created September 12, 2017 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattconsto/c5808093d9ce02f01c8fe5d49c106295 to your computer and use it in GitHub Desktop.
Save mattconsto/c5808093d9ce02f01c8fe5d49c106295 to your computer and use it in GitHub Desktop.
Date.prototype.format = function(string) {
// Helper function
var pad = function(width, string, padding) {
return (width <= (string + "").length) ? (string + "") : pad(width, (padding + "") + (string + ""), (padding + ""))
}
// Build data lazily, only evaluate when needed.
var date = this; // For scoping reasons.
var leap = ((date.getFullYear() % 4 == 0) && (date.getFullYear() % 100 != 0)) || (date.getFullYear() % 400 == 0);
var replacements = {
/* Day */
"d": function() {return pad(2, date.getDate(), 0)},
"D": function() {return ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][date.getDate()]},
"j": function() {return date.getDate()},
"l": function() {return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][date.getDate()]},
"N": function() {return date.getDay() != 0 ? date.getDay() : 7},
"S": function() {return ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"][(date.getDate() - 1) % 10]},
"w": function() {return date.getDay()},
"z": function() {return [0, 31, 31+ leap?29:28, 31+ leap?29:28 +31, 31+ leap?29:28 +31+30, 31+ leap?29:28 +31+30+31, 31+ leap?29:28 +31+30+31+30, 31+ leap?29:28 +31+30+31+30+31, 31+ leap?29:28 +31+30+31+30+31+31, 31+ leap?29:28 +31+30+31+30+31+31+30, 31+ leap?29:28 +31+30+31+30+31+31+30+31, 31+ leap?29:28 +31+30+31+30+31+31+30+31+30, 31+ leap?29:28 +31+30+31+30+31+31+30+31+30+31][date.getMonth()] + date.getDate() - 1},
/* Week */
"W": function() {return Math.ceil((((date - new Date(date.getFullYear(), 0, 4)) / 86400000) + new Date(date.getFullYear(), 0, 4).getDay() + 1) / 7)},
/* Month */
"F": function() {return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][date.getMonth()]},
"m": function() {return pad(2, date.getMonth() + 1, 0)},
"M": function() {return ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][date.getMonth()]},
"n": function() {return date.getMonth() + 1},
"t": function() {return [31, leap?29:28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][date.getMonth()]},
/* Year */
"L": function() {return leap ? 0 : 1},
"Y": function() {return date.getFullYear()},
"y": function() {return date.getFullYear() % 100},
/* Time */
"a": function() {return date.getHours() > 11 ? "pm" : "am"},
"A": function() {return date.getHours() > 11 ? "PM" : "AM"},
"B": function() {return (date.getHours() * 360 + date.getMinutes() * 60 + date.getSeconds() + 1) / 8640},
"g": function() {return date.getHours() % 12 + 1},
"G": function() {return date.getHours()},
"h": function() {return pad(2, date.getHours() % 12 + 1, 0)},
"H": function() {return pad(2, date.getHours(), 0)},
"i": function() {return pad(2, date.getMinutes(), 0)},
"s": function() {return pad(2, date.getSeconds(), 0)},
"v": function() {return date.getMilliseconds()},
/* Timezone */
"O": function() {return date.getTimezoneOffset() < 0 ? "-" : "+" + pad(2, Math.abs(date.getTimezoneOffset() / 60), 0) + pad(2, Math.abs(date.getTimezoneOffset() % 60), 0)},
"P": function() {return date.getTimezoneOffset() < 0 ? "-" : "+" + pad(2, Math.abs(date.getTimezoneOffset() / 60), 0) + ":" + pad(2, Math.abs(date.getTimezoneOffset() % 60), 0)},
"Z": function() {return date.getTimezoneOffset() * 60},
/* Full */
"c": function() {return date.toISOString()},
"U": function() {return date.now / 1000},
/* Other */
"%": "%"
}
// Format the string
string = string.split("");
var output = "";
for(var i = 0; i < string.length; i++) {
if(string[i] == "%" && string[i + 1] in replacements) {
// Lazy evaluation
if(typeof replacements[string[++i]] === "function") replacements[string[i]] = replacements[string[i]]();
output += replacements[string[i]];
} else {
output += string[i];
}
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment