Last active
December 17, 2015 07:49
-
-
Save dmiro/5576137 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.prototype.format = function(fstr, utc) { | |
| var that = this; | |
| utc = utc ? 'getUTC' : 'get'; | |
| return fstr.replace (/%[YmdHMS]/g, function (m) { | |
| switch (m) { | |
| case '%Y': return that[utc + 'FullYear'] (); | |
| case '%m': m = 1 + that[utc + 'Month'] (); break; | |
| case '%d': m = that[utc + 'Date'] (); break; | |
| case '%H': m = that[utc + 'Hours'] (); break; | |
| case '%M': m = that[utc + 'Minutes'] (); break; | |
| case '%S': m = that[utc + 'Seconds'] (); break; | |
| default: return m.slice (1); | |
| } | |
| return ('0' + m).slice (-2); | |
| }); | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// demo
a = new Date();
console.log(a.format ("%Y-%m-%d %H:%M:%S", true) );
console.log(a.format ("%d/%m/%Y %H:%M:%S", true));
console.log(a.format ("%d/%m/%Y %H:%M:%S", false));
console.log(a.format ("%m", true) ); // formated month
console.log(a.format ("%d", true) ); // formated date