1 | /* Port of strftime() by T. H. Doan (https://thdoan.github.io/strftime/) |
---|
2 | * |
---|
3 | * Day of year (%j) code based on Joe Orost's answer: |
---|
4 | * http://stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366 |
---|
5 | * |
---|
6 | * Week number (%V) code based on Taco van den Broek's prototype: |
---|
7 | * http://techblog.procurios.nl/k/news/view/33796/14863/calculate-iso-8601-week-and-year-in-javascript.html |
---|
8 | */ |
---|
9 | |
---|
10 | var EXPORTED_SYMBOLS = ["strftime"] |
---|
11 | |
---|
12 | function strftime(sFormat, date) { |
---|
13 | if (!(date instanceof Date)) date = new Date(); |
---|
14 | var nDay = date.getDay(), |
---|
15 | nDate = date.getDate(), |
---|
16 | nMonth = date.getMonth(), |
---|
17 | nYear = date.getFullYear(), |
---|
18 | nHour = date.getHours(), |
---|
19 | aDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], |
---|
20 | aMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], |
---|
21 | aDayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], |
---|
22 | isLeapYear = function() { |
---|
23 | return (nYear%4===0 && nYear%100!==0) || nYear%400===0; |
---|
24 | }, |
---|
25 | getThursday = function() { |
---|
26 | var target = new Date(date); |
---|
27 | target.setDate(nDate - ((nDay+6)%7) + 3); |
---|
28 | return target; |
---|
29 | }, |
---|
30 | zeroPad = function(nNum, nPad) { |
---|
31 | return ((Math.pow(10, nPad) + nNum) + '').slice(1); |
---|
32 | }; |
---|
33 | return sFormat.replace(/%[a-z]/gi, function(sMatch) { |
---|
34 | return (({ |
---|
35 | '%a': aDays[nDay].slice(0,3), |
---|
36 | '%A': aDays[nDay], |
---|
37 | '%b': aMonths[nMonth].slice(0,3), |
---|
38 | '%B': aMonths[nMonth], |
---|
39 | '%c': date.toUTCString(), |
---|
40 | '%C': Math.floor(nYear/100), |
---|
41 | '%d': zeroPad(nDate, 2), |
---|
42 | '%e': nDate, |
---|
43 | '%F': date.toISOString().slice(0,10), |
---|
44 | '%G': getThursday().getFullYear(), |
---|
45 | '%g': (getThursday().getFullYear() + '').slice(2), |
---|
46 | '%H': zeroPad(nHour, 2), |
---|
47 | '%I': zeroPad((nHour+11)%12 + 1, 2), |
---|
48 | '%j': zeroPad(aDayCount[nMonth] + nDate + ((nMonth>1 && isLeapYear()) ? 1 : 0), 3), |
---|
49 | '%k': nHour, |
---|
50 | '%l': (nHour+11)%12 + 1, |
---|
51 | '%m': zeroPad(nMonth + 1, 2), |
---|
52 | '%n': nMonth + 1, |
---|
53 | '%M': zeroPad(date.getMinutes(), 2), |
---|
54 | '%p': (nHour<12) ? 'AM' : 'PM', |
---|
55 | '%P': (nHour<12) ? 'am' : 'pm', |
---|
56 | '%s': Math.round(date.getTime()/1000), |
---|
57 | '%S': zeroPad(date.getSeconds(), 2), |
---|
58 | '%u': nDay || 7, |
---|
59 | '%V': (function() { |
---|
60 | var target = getThursday(), |
---|
61 | n1stThu = target.valueOf(); |
---|
62 | target.setMonth(0, 1); |
---|
63 | var nJan1 = target.getDay(); |
---|
64 | if (nJan1!==4) target.setMonth(0, 1 + ((4-nJan1)+7)%7); |
---|
65 | return zeroPad(1 + Math.ceil((n1stThu-target)/604800000), 2); |
---|
66 | })(), |
---|
67 | '%w': nDay, |
---|
68 | '%x': date.toLocaleDateString(), |
---|
69 | '%X': date.toLocaleTimeString(), |
---|
70 | '%y': (nYear + '').slice(2), |
---|
71 | '%Y': nYear, |
---|
72 | '%z': date.toTimeString().replace(/.+GMT([+-]\d+).+/, '$1'), |
---|
73 | '%Z': date.toTimeString().replace(/.+\((.+?)\)$/, '$1') |
---|
74 | }[sMatch] || '') + '') || sMatch; |
---|
75 | }); |
---|
76 | } |
---|