UNPKG

7.06 kBJavaScriptView Raw
1
2
3
4/*
5* @version 17.7.0
6* @author Lauri Rooden <lauri@rooden.ee>
7* @license MIT License
8*/
9
10
11
12!function(Date, proto) {
13 var Date$prototype = Date[proto]
14 , String$prototype = String[proto]
15 , Number$prototype = Number[proto]
16 , maskRe = /(\[)((?:\\?.)*?)\]|([YMD])\3\3\3?|([YMDHhmsWSZ])(\4?)|[uUASwoQ]|(["\\\n\r\u2028\u2029])/g
17 , dateRe = /(\d+)[-.\/](\d+)[-.\/](\d+)/
18 , timeRe = /(\d+):(\d+)(?::(\d+))?(\.\d+)?(?:\s*(?:(a)|(p))\.?m\.?)?(\s*(?:Z|GMT|UTC)?(?:([-+]\d\d):?(\d\d)?)?)?/i
19 , fns = Object.create(null)
20 , aliases = {
21 sec: "s",
22 second: "s",
23 seconds: "s",
24 min: "m",
25 minute: "m",
26 minutes: "m",
27 hr: "h",
28 hour: "h",
29 hours: "h",
30 day: "D",
31 days: "D",
32 week: "W",
33 weeks: "W",
34 month: "M",
35 months: "M",
36 year: "Y",
37 years: "Y"
38 }
39 , units = {
40 S: 1,
41 s: 1000,
42 m: 60000,
43 h: 3600000,
44 D: 86400000,
45 W: 604800000
46 }
47 , tmp1 = new Date()
48 , tmp2 = new Date()
49 , map = Date.fnMap = {
50 w: "Day()||7",
51 Y: "FullYear()%100",
52 M: "Month()+1",
53 D: "Date()",
54 h: "Hours()",
55 H: "Hours()%12||12",
56 m: "Minutes()",
57 s: "Seconds()",
58 S: "Milliseconds()"
59 }
60 , locales = Date.locales = {
61 en: {
62 am: "AM",
63 pm: "PM",
64 names: "JanFebMarAprMayJunJulAugSepOctNovDecJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecemberSunMonTueWedThuFriSatSundayMondayTuesdayWednesdayThursdayFridaySaturday".match(/.[a-z]+/g),
65 masks: {
66 LT: "hh:mm",
67 LTS: "hh:mm:ss",
68 L: "DD/MM/YYYY",
69 LL: "D MMMM YYYY",
70 LLL: "D MMMM YYYY hh:mm",
71 LLLL: "DDDD, D MMMM YYYY hh:mm"
72 }
73 }
74 }
75 , masks = Date.masks = {
76 "iso": "UTC:YYYY-MM-DD[T]hh:mm:ss[Z]"
77 }
78
79
80 Date.makeStr = makeStr
81 function makeStr(mask, utc) {
82 var get = "d.get" + (utc ? "UTC" : "")
83 , setA = "a.setTime(+d+((4-(" + get + map.w + "))*864e5))"
84 return (utc ? mask.slice(4) : mask).replace(maskRe, function(match, quote, text, MD, single, pad, esc) {
85 var str = (
86 esc ? escape(esc).replace(/%u/g, "\\u").replace(/%/g, "\\x") :
87 quote ? text :
88 MD == "Y" ? get + "FullYear()" :
89 MD ? "l.names[" + get + (MD == "M" ? "Month" : "Day" ) + "()+" + (match == "DDD" ? 24 : MD == "D" ? 31 : match == "MMM" ? 0 : 12) + "]" :
90 match == "u" ? "(d/1000)>>>0" :
91 match == "U" ? "+d" :
92 match == "Q" ? "((" + get + "Month()/3)|0)+1" :
93 match == "A" ? "l[" + get + map.h + ">11?'pm':'am']" :
94 match == "o" ? setA + ",a" + get.slice(1) + "FullYear()" :
95 single == "Z" ? "(t=o)?(t<0?((t=-t),'-'):'+')+(t<600?'0':'')+(0|(t/60))" + (pad ? "" : "+':'") + "+((t%=60)>9?t:'0'+t):'Z'" :
96 single == "W" ? "Math.ceil(((" + setA + "-a.s" + get.slice(3) + "Month(0,1))/864e5+1)/7)" :
97 get + map[single || match]
98 )
99 return quote || esc ? str : '"+(' + (
100 match == "SS" ? "(t=" + str + ")>9?t>99?t:'0'+t:'00'+t" :
101 pad && single != "Z" ? "(t=" + str + ")>9?t:'0'+t" :
102 str
103 ) + ')+"'
104 })
105 }
106
107 Date$prototype.date = function(_mask, _zone) {
108 var offset, undef
109 , date = this
110 , locale = locales[date._locale || Date._locale || "en"] || locales.en
111 , mask = locale.masks && locale.masks[_mask] || masks[_mask] || _mask || masks.iso
112 , zone = _zone == undef ? date._tz || Date._tz : _zone
113 , utc = mask.slice(0, 4) == "UTC:"
114 if (zone != undef && !utc) {
115 offset = 60 * zone
116 tmp1.setTime(+date + offset * 6e4)
117 utc = mask = "UTC:" + mask
118 } else {
119 offset = utc ? 0 : -date.getTimezoneOffset()
120 tmp1.setTime(+date)
121 }
122 return isNaN(+date) ? "" + date : (
123 fns[mask] || (fns[mask] = Function("d,a,o,l", 'var t;return "' + makeStr(mask, utc) + '"')))(
124 tmp1,
125 tmp2,
126 offset,
127 locale
128 )
129 }
130
131 addFn("add", function(amount, _unit, mask) {
132 var date = this
133 , unit = aliases[_unit] || _unit
134 if (unit == "M" || unit == "Y" && (amount *= 12)) {
135 unit = date.getUTCDate()
136 date.setUTCMonth(date.getUTCMonth() + amount)
137 if (unit > (unit = date.getUTCDate())) {
138 date.add(-unit, "D")
139 }
140 } else if (amount) {
141 date.setTime(date.getTime() + (amount * (units[unit] || 1)))
142 }
143 return mask ? date.date(mask) : date
144 })
145
146 addFn("startOf", function(_unit, mask) {
147 var date = this
148 , unit = aliases[_unit] || _unit
149 if (unit == "Y") {
150 date.setUTCMonth(0, 1)
151 unit = "D"
152 } else if (unit == "M") {
153 date.setUTCDate(1)
154 unit = "D"
155 }
156 date.setTime(date - (date % (units[unit] || 1)))
157 return mask ? date.date(mask) : date
158 })
159
160 addFn("endOf", function(unit, mask) {
161 return this.startOf(unit).add(1, unit).add(-1, "S", mask)
162 })
163
164 addFn("since", function(from, _unit) {
165 var diff
166 , date = this
167 , unit = aliases[_unit] || _unit
168 if (typeof from == "string") {
169 from = aliases[from] ? (tmp2.setTime(+date), tmp2.startOf(from)) : from.date()
170 }
171 if (units[unit]) {
172 diff = (date - from) / units[unit]
173 } else {
174 diff = date.since("month", "S") - from.since("month", "S")
175 if (diff) {
176 tmp1.setTime(+date)
177 diff /= units.D * tmp1.endOf("M").getUTCDate()
178 }
179 diff += 12 * (date.getUTCFullYear() - from.getUTCFullYear()) + date.getUTCMonth() - from.getUTCMonth()
180 if (unit == "Y") {
181 diff /= 12
182 }
183 }
184
185 return diff
186 })
187
188 //*/
189
190
191 /*
192 * // In Chrome Date.parse("01.02.2001") is Jan
193 * num = +date || Date.parse(date) || ""+date;
194 */
195
196 String$prototype.date = function(mask, zoneOut, zoneIn) {
197 var undef, date, match, year, month
198 , str = this
199 if (isNaN(+str)) {
200 if (match = str.match(dateRe)) {
201 // Big endian date, starting with the year, eg. 2011-01-31
202 // Middle endian date, starting with the month, eg. 01/31/2011
203 // Little endian date, starting with the day, eg. 31.01.2011
204 year = match[1] > 99 ? 1 : 3
205 month = Date.middleEndian ? 4 - year : 2
206 date = new Date(match[year], match[month] - 1, match[6 - month - year])
207 } else {
208 date = new Date()
209 }
210
211 // Time
212 match = str.match(timeRe) || [0, 0, 0]
213 date.setHours(
214 match[6] && match[1] < 12 ? +match[1] + 12 :
215 match[5] && match[1] == 12 ? 0 : match[1],
216 match[2], match[3]|0, (1000 * match[4])|0
217 )
218
219 // Timezone
220 if (match[7]) {
221 zoneIn = (match[8]|0) + ((match[9]|0)/(match[8]<0?-60:60))
222 }
223
224 if (zoneIn != undef) {
225 date.setTime(date - (60 * zoneIn + date.getTimezoneOffset()) * 60000)
226 }
227 return mask ? date.date(mask, zoneOut) : date
228 }
229 return (+str).date(mask, zoneOut, zoneIn)
230 }
231
232 Number$prototype.date = function(mask, zoneOut, zoneIn) {
233 var date
234 , num = this
235 if (num < 4294967296) num *= 1000
236 date = new Date(
237 zoneIn != date ?
238 tmp1.setTime(num) - (60 * zoneIn + tmp1.getTimezoneOffset()) * 60000 :
239 num
240 )
241
242 return mask ? date.date(mask, zoneOut) : date
243 }
244
245 function addFn(method, fn) {
246 Date$prototype[method] = fn
247 String$prototype[method] = Number$prototype[method] = function(a, b, c) {
248 return this.date()[method](a, b, c)
249 }
250 }
251
252 function makeSetter(method) {
253 Date[method] = Date$prototype[method] = function(value, mask) {
254 var date = this
255 date["_" + method] = value
256 return mask ? date.date(mask) : date
257 }
258 }
259
260 makeSetter("tz")
261 makeSetter("locale")
262}(Date, "prototype")
263
264
265
266