day.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. export default (function () {
  2. 'use strict'
  3. var t = 1e3,
  4. e = 6e4,
  5. n = 36e5,
  6. r = 'millisecond',
  7. i = 'second',
  8. s = 'minute',
  9. u = 'hour',
  10. a = 'day',
  11. o = 'week',
  12. c = 'month',
  13. f = 'quarter',
  14. h = 'year',
  15. d = 'date',
  16. l = 'Invalid Date',
  17. $ =
  18. /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/,
  19. y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,
  20. M = {
  21. name: 'en',
  22. weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
  23. months:
  24. 'January_February_March_April_May_June_July_August_September_October_November_December'.split(
  25. '_'
  26. ),
  27. ordinal: function (t) {
  28. var e = ['th', 'st', 'nd', 'rd'],
  29. n = t % 100
  30. return '[' + t + (e[(n - 20) % 10] || e[n] || e[0]) + ']'
  31. }
  32. },
  33. m = function (t, e, n) {
  34. var r = String(t)
  35. return !r || r.length >= e ? t : '' + Array(e + 1 - r.length).join(n) + t
  36. },
  37. v = {
  38. s: m,
  39. z: function (t) {
  40. var e = -t.utcOffset(),
  41. n = Math.abs(e),
  42. r = Math.floor(n / 60),
  43. i = n % 60
  44. return (e <= 0 ? '+' : '-') + m(r, 2, '0') + ':' + m(i, 2, '0')
  45. },
  46. m: function t(e, n) {
  47. if (e.date() < n.date()) return -t(n, e)
  48. var r = 12 * (n.year() - e.year()) + (n.month() - e.month()),
  49. i = e.clone().add(r, c),
  50. s = n - i < 0,
  51. u = e.clone().add(r + (s ? -1 : 1), c)
  52. return +(-(r + (n - i) / (s ? i - u : u - i)) || 0)
  53. },
  54. a: function (t) {
  55. return t < 0 ? Math.ceil(t) || 0 : Math.floor(t)
  56. },
  57. p: function (t) {
  58. return (
  59. { M: c, y: h, w: o, d: a, D: d, h: u, m: s, s: i, ms: r, Q: f }[t] ||
  60. String(t || '')
  61. .toLowerCase()
  62. .replace(/s$/, '')
  63. )
  64. },
  65. u: function (t) {
  66. return void 0 === t
  67. }
  68. },
  69. g = 'en',
  70. D = {}
  71. D[g] = M
  72. var p = '$isDayjsObject',
  73. S = function (t) {
  74. return t instanceof _ || !(!t || !t[p])
  75. },
  76. w = function t(e, n, r) {
  77. var i
  78. if (!e) return g
  79. if ('string' == typeof e) {
  80. var s = e.toLowerCase()
  81. D[s] && (i = s), n && ((D[s] = n), (i = s))
  82. var u = e.split('-')
  83. if (!i && u.length > 1) return t(u[0])
  84. } else {
  85. var a = e.name
  86. ;(D[a] = e), (i = a)
  87. }
  88. return !r && i && (g = i), i || (!r && g)
  89. },
  90. O = function (t, e) {
  91. if (S(t)) return t.clone()
  92. var n = 'object' == typeof e ? e : {}
  93. return (n.date = t), (n.args = arguments), new _(n)
  94. },
  95. b = v
  96. ;(b.l = w),
  97. (b.i = S),
  98. (b.w = function (t, e) {
  99. return O(t, { locale: e.$L, utc: e.$u, x: e.$x, $offset: e.$offset })
  100. })
  101. var _ = (function () {
  102. function M(t) {
  103. ;(this.$L = w(t.locale, null, !0)),
  104. this.parse(t),
  105. (this.$x = this.$x || t.x || {}),
  106. (this[p] = !0)
  107. }
  108. var m = M.prototype
  109. return (
  110. (m.parse = function (t) {
  111. ;(this.$d = (function (t) {
  112. var e = t.date,
  113. n = t.utc
  114. if (null === e) return new Date(NaN)
  115. if (b.u(e)) return new Date()
  116. if (e instanceof Date) return new Date(e)
  117. if ('string' == typeof e && !/Z$/i.test(e)) {
  118. var r = e.match($)
  119. if (r) {
  120. var i = r[2] - 1 || 0,
  121. s = (r[7] || '0').substring(0, 3)
  122. return n
  123. ? new Date(Date.UTC(r[1], i, r[3] || 1, r[4] || 0, r[5] || 0, r[6] || 0, s))
  124. : new Date(r[1], i, r[3] || 1, r[4] || 0, r[5] || 0, r[6] || 0, s)
  125. }
  126. }
  127. return new Date(e)
  128. })(t)),
  129. this.init()
  130. }),
  131. (m.init = function () {
  132. var t = this.$d
  133. ;(this.$y = t.getFullYear()),
  134. (this.$M = t.getMonth()),
  135. (this.$D = t.getDate()),
  136. (this.$W = t.getDay()),
  137. (this.$H = t.getHours()),
  138. (this.$m = t.getMinutes()),
  139. (this.$s = t.getSeconds()),
  140. (this.$ms = t.getMilliseconds())
  141. }),
  142. (m.$utils = function () {
  143. return b
  144. }),
  145. (m.isValid = function () {
  146. return !(this.$d.toString() === l)
  147. }),
  148. (m.isSame = function (t, e) {
  149. var n = O(t)
  150. return this.startOf(e) <= n && n <= this.endOf(e)
  151. }),
  152. (m.isAfter = function (t, e) {
  153. return O(t) < this.startOf(e)
  154. }),
  155. (m.isBefore = function (t, e) {
  156. return this.endOf(e) < O(t)
  157. }),
  158. (m.$g = function (t, e, n) {
  159. return b.u(t) ? this[e] : this.set(n, t)
  160. }),
  161. (m.unix = function () {
  162. return Math.floor(this.valueOf() / 1e3)
  163. }),
  164. (m.valueOf = function () {
  165. return this.$d.getTime()
  166. }),
  167. (m.startOf = function (t, e) {
  168. var n = this,
  169. r = !!b.u(e) || e,
  170. f = b.p(t),
  171. l = function (t, e) {
  172. var i = b.w(n.$u ? Date.UTC(n.$y, e, t) : new Date(n.$y, e, t), n)
  173. return r ? i : i.endOf(a)
  174. },
  175. $ = function (t, e) {
  176. return b.w(
  177. n.toDate()[t].apply(n.toDate('s'), (r ? [0, 0, 0, 0] : [23, 59, 59, 999]).slice(e)),
  178. n
  179. )
  180. },
  181. y = this.$W,
  182. M = this.$M,
  183. m = this.$D,
  184. v = 'set' + (this.$u ? 'UTC' : '')
  185. switch (f) {
  186. case h:
  187. return r ? l(1, 0) : l(31, 11)
  188. case c:
  189. return r ? l(1, M) : l(0, M + 1)
  190. case o:
  191. var g = this.$locale().weekStart || 0,
  192. D = (y < g ? y + 7 : y) - g
  193. return l(r ? m - D : m + (6 - D), M)
  194. case a:
  195. case d:
  196. return $(v + 'Hours', 0)
  197. case u:
  198. return $(v + 'Minutes', 1)
  199. case s:
  200. return $(v + 'Seconds', 2)
  201. case i:
  202. return $(v + 'Milliseconds', 3)
  203. default:
  204. return this.clone()
  205. }
  206. }),
  207. (m.endOf = function (t) {
  208. return this.startOf(t, !1)
  209. }),
  210. (m.$set = function (t, e) {
  211. var n,
  212. o = b.p(t),
  213. f = 'set' + (this.$u ? 'UTC' : ''),
  214. l = ((n = {}),
  215. (n[a] = f + 'Date'),
  216. (n[d] = f + 'Date'),
  217. (n[c] = f + 'Month'),
  218. (n[h] = f + 'FullYear'),
  219. (n[u] = f + 'Hours'),
  220. (n[s] = f + 'Minutes'),
  221. (n[i] = f + 'Seconds'),
  222. (n[r] = f + 'Milliseconds'),
  223. n)[o],
  224. $ = o === a ? this.$D + (e - this.$W) : e
  225. if (o === c || o === h) {
  226. var y = this.clone().set(d, 1)
  227. y.$d[l]($), y.init(), (this.$d = y.set(d, Math.min(this.$D, y.daysInMonth())).$d)
  228. } else l && this.$d[l]($)
  229. return this.init(), this
  230. }),
  231. (m.set = function (t, e) {
  232. return this.clone().$set(t, e)
  233. }),
  234. (m.get = function (t) {
  235. return this[b.p(t)]()
  236. }),
  237. (m.add = function (r, f) {
  238. var d,
  239. l = this
  240. r = Number(r)
  241. var $ = b.p(f),
  242. y = function (t) {
  243. var e = O(l)
  244. return b.w(e.date(e.date() + Math.round(t * r)), l)
  245. }
  246. if ($ === c) return this.set(c, this.$M + r)
  247. if ($ === h) return this.set(h, this.$y + r)
  248. if ($ === a) return y(1)
  249. if ($ === o) return y(7)
  250. var M = ((d = {}), (d[s] = e), (d[u] = n), (d[i] = t), d)[$] || 1,
  251. m = this.$d.getTime() + r * M
  252. return b.w(m, this)
  253. }),
  254. (m.subtract = function (t, e) {
  255. return this.add(-1 * t, e)
  256. }),
  257. (m.format = function (t) {
  258. var e = this,
  259. n = this.$locale()
  260. if (!this.isValid()) return n.invalidDate || l
  261. var r = t || 'YYYY-MM-DDTHH:mm:ssZ',
  262. i = b.z(this),
  263. s = this.$H,
  264. u = this.$m,
  265. a = this.$M,
  266. o = n.weekdays,
  267. c = n.months,
  268. f = n.meridiem,
  269. h = function (t, n, i, s) {
  270. return (t && (t[n] || t(e, r))) || i[n].slice(0, s)
  271. },
  272. d = function (t) {
  273. return b.s(s % 12 || 12, t, '0')
  274. },
  275. $ =
  276. f ||
  277. function (t, e, n) {
  278. var r = t < 12 ? 'AM' : 'PM'
  279. return n ? r.toLowerCase() : r
  280. }
  281. return r.replace(y, function (t, r) {
  282. return (
  283. r ||
  284. (function (t) {
  285. switch (t) {
  286. case 'YY':
  287. return String(e.$y).slice(-2)
  288. case 'YYYY':
  289. return b.s(e.$y, 4, '0')
  290. case 'M':
  291. return a + 1
  292. case 'MM':
  293. return b.s(a + 1, 2, '0')
  294. case 'MMM':
  295. return h(n.monthsShort, a, c, 3)
  296. case 'MMMM':
  297. return h(c, a)
  298. case 'D':
  299. return e.$D
  300. case 'DD':
  301. return b.s(e.$D, 2, '0')
  302. case 'd':
  303. return String(e.$W)
  304. case 'dd':
  305. return h(n.weekdaysMin, e.$W, o, 2)
  306. case 'ddd':
  307. return h(n.weekdaysShort, e.$W, o, 3)
  308. case 'dddd':
  309. return o[e.$W]
  310. case 'H':
  311. return String(s)
  312. case 'HH':
  313. return b.s(s, 2, '0')
  314. case 'h':
  315. return d(1)
  316. case 'hh':
  317. return d(2)
  318. case 'a':
  319. return $(s, u, !0)
  320. case 'A':
  321. return $(s, u, !1)
  322. case 'm':
  323. return String(u)
  324. case 'mm':
  325. return b.s(u, 2, '0')
  326. case 's':
  327. return String(e.$s)
  328. case 'ss':
  329. return b.s(e.$s, 2, '0')
  330. case 'SSS':
  331. return b.s(e.$ms, 3, '0')
  332. case 'Z':
  333. return i
  334. }
  335. return null
  336. })(t) ||
  337. i.replace(':', '')
  338. )
  339. })
  340. }),
  341. (m.utcOffset = function () {
  342. return 15 * -Math.round(this.$d.getTimezoneOffset() / 15)
  343. }),
  344. (m.diff = function (r, d, l) {
  345. var $,
  346. y = this,
  347. M = b.p(d),
  348. m = O(r),
  349. v = (m.utcOffset() - this.utcOffset()) * e,
  350. g = this - m,
  351. D = function () {
  352. return b.m(y, m)
  353. }
  354. switch (M) {
  355. case h:
  356. $ = D() / 12
  357. break
  358. case c:
  359. $ = D()
  360. break
  361. case f:
  362. $ = D() / 3
  363. break
  364. case o:
  365. $ = (g - v) / 6048e5
  366. break
  367. case a:
  368. $ = (g - v) / 864e5
  369. break
  370. case u:
  371. $ = g / n
  372. break
  373. case s:
  374. $ = g / e
  375. break
  376. case i:
  377. $ = g / t
  378. break
  379. default:
  380. $ = g
  381. }
  382. return l ? $ : b.a($)
  383. }),
  384. (m.daysInMonth = function () {
  385. return this.endOf(c).$D
  386. }),
  387. (m.$locale = function () {
  388. return D[this.$L]
  389. }),
  390. (m.locale = function (t, e) {
  391. if (!t) return this.$L
  392. var n = this.clone(),
  393. r = w(t, e, !0)
  394. return r && (n.$L = r), n
  395. }),
  396. (m.clone = function () {
  397. return b.w(this.$d, this)
  398. }),
  399. (m.toDate = function () {
  400. return new Date(this.valueOf())
  401. }),
  402. (m.toJSON = function () {
  403. return this.isValid() ? this.toISOString() : null
  404. }),
  405. (m.toISOString = function () {
  406. return this.$d.toISOString()
  407. }),
  408. (m.toString = function () {
  409. return this.$d.toUTCString()
  410. }),
  411. M
  412. )
  413. })(),
  414. k = _.prototype
  415. return (
  416. (O.prototype = k),
  417. [
  418. ['$ms', r],
  419. ['$s', i],
  420. ['$m', s],
  421. ['$H', u],
  422. ['$W', a],
  423. ['$M', c],
  424. ['$y', h],
  425. ['$D', d]
  426. ].forEach(function (t) {
  427. k[t[1]] = function (e) {
  428. return this.$g(e, t[0], t[1])
  429. }
  430. }),
  431. (O.extend = function (t, e) {
  432. return t.$i || (t(e, _, O), (t.$i = !0)), O
  433. }),
  434. (O.locale = w),
  435. (O.isDayjs = S),
  436. (O.unix = function (t) {
  437. return O(1e3 * t)
  438. }),
  439. (O.en = D[g]),
  440. (O.Ls = D),
  441. (O.p = {}),
  442. O
  443. )
  444. })()