-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatdate.js
57 lines (52 loc) · 3.17 KB
/
formatdate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var formatDate = function(date, format, utc){
var pad = function(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
};
if(utc == null)
utc = false;
var formats = {
'%a': function(){return ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][utc ? date.getUTCDay() : date.getDay()];},
'%b': function(){return ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][utc ? date.getUTCMonth() : date.getMonth()];},
'%c': function(){return utc ? date.getUTCMonth() + 1 : date.getMonth() + 1;},
'%D': function(){
var t, n=this["%e"](), nn=parseInt(n.toString().substr(-2));
return nn>3&&nn<21?n+"th":t=["th","st","nd","rd"][n.toString().substr(-1)],n+(t==null?"th":t);
},
'%d': function(){return pad(utc?date.getUTCDate():date.getDate(),2);},
'%e': function(){return utc?date.getUTCDate():date.getDate();},
'%f': function(){return pad(utc?date.getUTCMilliseconds():date.getMilliseconds(),3);},
'%H': function(){return pad(this["%k"](), 2);},
'%h': function(){return pad(this["%l"](), 2);},
'%I': function(){return this["%h"]();},
'%i': function(){return pad(utc ? date.getUTCMinutes() : date.getMinutes(), 2)},
'%j': function(){/*Day of year (001-266)*/},
'%k': function(){return utc?date.getUTCHours():date.getHours()},
'%l': function(){var x; return x = this["%k"](), x = x == 0 ? x = 12 : x, x > 12 ? x -12 : x},
'%M': function(){return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][utc ? date.getUTCMonth() : date.getMonth()];},
'%m': function(){return pad(this["%c"](), 2);},
'%p': function(){return this["%H"]() > 11 ? "PM" : "AM";},
'%r': function(){return this["%h"]() + ":" + this["%i"]() + ":" + this["%S"]() + " " + this["%p"]()},
'%S': function(){return pad(utc?date.getUTCSeconds():date.getSeconds(),2);},
'%s': function(){return this["%S"]();},
'%T': function(){return this["%H"]()+":"+this["%i"]()+":" + this["%S"]();},
'%U': function(){/*Week (00..53), where Sunday is the first day of the week*/},
'%u': function(){/*Week (00..53), where Monday is the first day of the week*/},
'%V': function(){/*Week (01..53), where Sunday is the first day of the week; used with %X*/},
'%v': function(){/*Week (01..53), where Monday is the first day of the week; used with %x*/},
'%W': function(){return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][utc ? date.getUTCDay() : date.getDay()];},
'%w': function(){return utc?date.getUTCDay():date.getDay();},
'%X': function(){/*Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V*/},
'%x': function(){/*Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v*/},
'%Y': function(){return utc?date.getUTCFullYear():date.getFullYear();},
'%y': function(){return (utc?date.getUTCFullYear():date.getFullYear()).toString().substring(2);},
'%%': function(){return "%"}
};
var replacer = function(match){
if(formats[match] == null)
return match.substr(1,1);
return formats[match]();
}
return format.replace(/\%.?/g, replacer);
};