index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import * as C from './constant';
  2. import en from './locale/en';
  3. import U from './utils';
  4. var L = 'en'; // global locale
  5. var Ls = {}; // global loaded locale
  6. Ls[L] = en;
  7. var IS_DAYJS = '$isDayjsObject'; // eslint-disable-next-line no-use-before-define
  8. var isDayjs = function isDayjs(d) {
  9. return d instanceof Dayjs || !!(d && d[IS_DAYJS]);
  10. };
  11. var parseLocale = function parseLocale(preset, object, isLocal) {
  12. var l;
  13. if (!preset) return L;
  14. if (typeof preset === 'string') {
  15. var presetLower = preset.toLowerCase();
  16. if (Ls[presetLower]) {
  17. l = presetLower;
  18. }
  19. if (object) {
  20. Ls[presetLower] = object;
  21. l = presetLower;
  22. }
  23. var presetSplit = preset.split('-');
  24. if (!l && presetSplit.length > 1) {
  25. return parseLocale(presetSplit[0]);
  26. }
  27. } else {
  28. var name = preset.name;
  29. Ls[name] = preset;
  30. l = name;
  31. }
  32. if (!isLocal && l) L = l;
  33. return l || !isLocal && L;
  34. };
  35. var dayjs = function dayjs(date, c) {
  36. if (isDayjs(date)) {
  37. return date.clone();
  38. } // eslint-disable-next-line no-nested-ternary
  39. var cfg = typeof c === 'object' ? c : {};
  40. cfg.date = date;
  41. cfg.args = arguments; // eslint-disable-line prefer-rest-params
  42. return new Dayjs(cfg); // eslint-disable-line no-use-before-define
  43. };
  44. var wrapper = function wrapper(date, instance) {
  45. return dayjs(date, {
  46. locale: instance.$L,
  47. utc: instance.$u,
  48. x: instance.$x,
  49. $offset: instance.$offset // todo: refactor; do not use this.$offset in you code
  50. });
  51. };
  52. var Utils = U; // for plugin use
  53. Utils.l = parseLocale;
  54. Utils.i = isDayjs;
  55. Utils.w = wrapper;
  56. var parseDate = function parseDate(cfg) {
  57. var date = cfg.date,
  58. utc = cfg.utc;
  59. if (date === null) return new Date(NaN); // null is invalid
  60. if (Utils.u(date)) return new Date(); // today
  61. if (date instanceof Date) return new Date(date);
  62. if (typeof date === 'string' && !/Z$/i.test(date)) {
  63. var d = date.match(C.REGEX_PARSE);
  64. if (d) {
  65. var m = d[2] - 1 || 0;
  66. var ms = (d[7] || '0').substring(0, 3);
  67. if (utc) {
  68. return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
  69. }
  70. return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
  71. }
  72. }
  73. return new Date(date); // everything else
  74. };
  75. var Dayjs = /*#__PURE__*/function () {
  76. function Dayjs(cfg) {
  77. this.$L = parseLocale(cfg.locale, null, true);
  78. this.parse(cfg); // for plugin
  79. this.$x = this.$x || cfg.x || {};
  80. this[IS_DAYJS] = true;
  81. }
  82. var _proto = Dayjs.prototype;
  83. _proto.parse = function parse(cfg) {
  84. this.$d = parseDate(cfg);
  85. this.init();
  86. };
  87. _proto.init = function init() {
  88. var $d = this.$d;
  89. this.$y = $d.getFullYear();
  90. this.$M = $d.getMonth();
  91. this.$D = $d.getDate();
  92. this.$W = $d.getDay();
  93. this.$H = $d.getHours();
  94. this.$m = $d.getMinutes();
  95. this.$s = $d.getSeconds();
  96. this.$ms = $d.getMilliseconds();
  97. } // eslint-disable-next-line class-methods-use-this
  98. ;
  99. _proto.$utils = function $utils() {
  100. return Utils;
  101. };
  102. _proto.isValid = function isValid() {
  103. return !(this.$d.toString() === C.INVALID_DATE_STRING);
  104. };
  105. _proto.isSame = function isSame(that, units) {
  106. var other = dayjs(that);
  107. return this.startOf(units) <= other && other <= this.endOf(units);
  108. };
  109. _proto.isAfter = function isAfter(that, units) {
  110. return dayjs(that) < this.startOf(units);
  111. };
  112. _proto.isBefore = function isBefore(that, units) {
  113. return this.endOf(units) < dayjs(that);
  114. };
  115. _proto.$g = function $g(input, get, set) {
  116. if (Utils.u(input)) return this[get];
  117. return this.set(set, input);
  118. };
  119. _proto.unix = function unix() {
  120. return Math.floor(this.valueOf() / 1000);
  121. };
  122. _proto.valueOf = function valueOf() {
  123. // timezone(hour) * 60 * 60 * 1000 => ms
  124. return this.$d.getTime();
  125. };
  126. _proto.startOf = function startOf(units, _startOf) {
  127. var _this = this;
  128. // startOf -> endOf
  129. var isStartOf = !Utils.u(_startOf) ? _startOf : true;
  130. var unit = Utils.p(units);
  131. var instanceFactory = function instanceFactory(d, m) {
  132. var ins = Utils.w(_this.$u ? Date.UTC(_this.$y, m, d) : new Date(_this.$y, m, d), _this);
  133. return isStartOf ? ins : ins.endOf(C.D);
  134. };
  135. var instanceFactorySet = function instanceFactorySet(method, slice) {
  136. var argumentStart = [0, 0, 0, 0];
  137. var argumentEnd = [23, 59, 59, 999];
  138. return Utils.w(_this.toDate()[method].apply( // eslint-disable-line prefer-spread
  139. _this.toDate('s'), (isStartOf ? argumentStart : argumentEnd).slice(slice)), _this);
  140. };
  141. var $W = this.$W,
  142. $M = this.$M,
  143. $D = this.$D;
  144. var utcPad = "set" + (this.$u ? 'UTC' : '');
  145. switch (unit) {
  146. case C.Y:
  147. return isStartOf ? instanceFactory(1, 0) : instanceFactory(31, 11);
  148. case C.M:
  149. return isStartOf ? instanceFactory(1, $M) : instanceFactory(0, $M + 1);
  150. case C.W:
  151. {
  152. var weekStart = this.$locale().weekStart || 0;
  153. var gap = ($W < weekStart ? $W + 7 : $W) - weekStart;
  154. return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M);
  155. }
  156. case C.D:
  157. case C.DATE:
  158. return instanceFactorySet(utcPad + "Hours", 0);
  159. case C.H:
  160. return instanceFactorySet(utcPad + "Minutes", 1);
  161. case C.MIN:
  162. return instanceFactorySet(utcPad + "Seconds", 2);
  163. case C.S:
  164. return instanceFactorySet(utcPad + "Milliseconds", 3);
  165. default:
  166. return this.clone();
  167. }
  168. };
  169. _proto.endOf = function endOf(arg) {
  170. return this.startOf(arg, false);
  171. };
  172. _proto.$set = function $set(units, _int) {
  173. var _C$D$C$DATE$C$M$C$Y$C;
  174. // private set
  175. var unit = Utils.p(units);
  176. var utcPad = "set" + (this.$u ? 'UTC' : '');
  177. var name = (_C$D$C$DATE$C$M$C$Y$C = {}, _C$D$C$DATE$C$M$C$Y$C[C.D] = utcPad + "Date", _C$D$C$DATE$C$M$C$Y$C[C.DATE] = utcPad + "Date", _C$D$C$DATE$C$M$C$Y$C[C.M] = utcPad + "Month", _C$D$C$DATE$C$M$C$Y$C[C.Y] = utcPad + "FullYear", _C$D$C$DATE$C$M$C$Y$C[C.H] = utcPad + "Hours", _C$D$C$DATE$C$M$C$Y$C[C.MIN] = utcPad + "Minutes", _C$D$C$DATE$C$M$C$Y$C[C.S] = utcPad + "Seconds", _C$D$C$DATE$C$M$C$Y$C[C.MS] = utcPad + "Milliseconds", _C$D$C$DATE$C$M$C$Y$C)[unit];
  178. var arg = unit === C.D ? this.$D + (_int - this.$W) : _int;
  179. if (unit === C.M || unit === C.Y) {
  180. // clone is for badMutable plugin
  181. var date = this.clone().set(C.DATE, 1);
  182. date.$d[name](arg);
  183. date.init();
  184. this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).$d;
  185. } else if (name) this.$d[name](arg);
  186. this.init();
  187. return this;
  188. };
  189. _proto.set = function set(string, _int2) {
  190. return this.clone().$set(string, _int2);
  191. };
  192. _proto.get = function get(unit) {
  193. return this[Utils.p(unit)]();
  194. };
  195. _proto.add = function add(number, units) {
  196. var _this2 = this,
  197. _C$MIN$C$H$C$S$unit;
  198. number = Number(number); // eslint-disable-line no-param-reassign
  199. var unit = Utils.p(units);
  200. var instanceFactorySet = function instanceFactorySet(n) {
  201. var d = dayjs(_this2);
  202. return Utils.w(d.date(d.date() + Math.round(n * number)), _this2);
  203. };
  204. if (unit === C.M) {
  205. return this.set(C.M, this.$M + number);
  206. }
  207. if (unit === C.Y) {
  208. return this.set(C.Y, this.$y + number);
  209. }
  210. if (unit === C.D) {
  211. return instanceFactorySet(1);
  212. }
  213. if (unit === C.W) {
  214. return instanceFactorySet(7);
  215. }
  216. var step = (_C$MIN$C$H$C$S$unit = {}, _C$MIN$C$H$C$S$unit[C.MIN] = C.MILLISECONDS_A_MINUTE, _C$MIN$C$H$C$S$unit[C.H] = C.MILLISECONDS_A_HOUR, _C$MIN$C$H$C$S$unit[C.S] = C.MILLISECONDS_A_SECOND, _C$MIN$C$H$C$S$unit)[unit] || 1; // ms
  217. var nextTimeStamp = this.$d.getTime() + number * step;
  218. return Utils.w(nextTimeStamp, this);
  219. };
  220. _proto.subtract = function subtract(number, string) {
  221. return this.add(number * -1, string);
  222. };
  223. _proto.format = function format(formatStr) {
  224. var _this3 = this;
  225. var locale = this.$locale();
  226. if (!this.isValid()) return locale.invalidDate || C.INVALID_DATE_STRING;
  227. var str = formatStr || C.FORMAT_DEFAULT;
  228. var zoneStr = Utils.z(this);
  229. var $H = this.$H,
  230. $m = this.$m,
  231. $M = this.$M;
  232. var weekdays = locale.weekdays,
  233. months = locale.months,
  234. meridiem = locale.meridiem;
  235. var getShort = function getShort(arr, index, full, length) {
  236. return arr && (arr[index] || arr(_this3, str)) || full[index].slice(0, length);
  237. };
  238. var get$H = function get$H(num) {
  239. return Utils.s($H % 12 || 12, num, '0');
  240. };
  241. var meridiemFunc = meridiem || function (hour, minute, isLowercase) {
  242. var m = hour < 12 ? 'AM' : 'PM';
  243. return isLowercase ? m.toLowerCase() : m;
  244. };
  245. var matches = function matches(match) {
  246. switch (match) {
  247. case 'YY':
  248. return String(_this3.$y).slice(-2);
  249. case 'YYYY':
  250. return Utils.s(_this3.$y, 4, '0');
  251. case 'M':
  252. return $M + 1;
  253. case 'MM':
  254. return Utils.s($M + 1, 2, '0');
  255. case 'MMM':
  256. return getShort(locale.monthsShort, $M, months, 3);
  257. case 'MMMM':
  258. return getShort(months, $M);
  259. case 'D':
  260. return _this3.$D;
  261. case 'DD':
  262. return Utils.s(_this3.$D, 2, '0');
  263. case 'd':
  264. return String(_this3.$W);
  265. case 'dd':
  266. return getShort(locale.weekdaysMin, _this3.$W, weekdays, 2);
  267. case 'ddd':
  268. return getShort(locale.weekdaysShort, _this3.$W, weekdays, 3);
  269. case 'dddd':
  270. return weekdays[_this3.$W];
  271. case 'H':
  272. return String($H);
  273. case 'HH':
  274. return Utils.s($H, 2, '0');
  275. case 'h':
  276. return get$H(1);
  277. case 'hh':
  278. return get$H(2);
  279. case 'a':
  280. return meridiemFunc($H, $m, true);
  281. case 'A':
  282. return meridiemFunc($H, $m, false);
  283. case 'm':
  284. return String($m);
  285. case 'mm':
  286. return Utils.s($m, 2, '0');
  287. case 's':
  288. return String(_this3.$s);
  289. case 'ss':
  290. return Utils.s(_this3.$s, 2, '0');
  291. case 'SSS':
  292. return Utils.s(_this3.$ms, 3, '0');
  293. case 'Z':
  294. return zoneStr;
  295. // 'ZZ' logic below
  296. default:
  297. break;
  298. }
  299. return null;
  300. };
  301. return str.replace(C.REGEX_FORMAT, function (match, $1) {
  302. return $1 || matches(match) || zoneStr.replace(':', '');
  303. }); // 'ZZ'
  304. };
  305. _proto.utcOffset = function utcOffset() {
  306. // Because a bug at FF24, we're rounding the timezone offset around 15 minutes
  307. // https://github.com/moment/moment/pull/1871
  308. return -Math.round(this.$d.getTimezoneOffset() / 15) * 15;
  309. };
  310. _proto.diff = function diff(input, units, _float) {
  311. var _this4 = this;
  312. var unit = Utils.p(units);
  313. var that = dayjs(input);
  314. var zoneDelta = (that.utcOffset() - this.utcOffset()) * C.MILLISECONDS_A_MINUTE;
  315. var diff = this - that;
  316. var getMonth = function getMonth() {
  317. return Utils.m(_this4, that);
  318. };
  319. var result;
  320. switch (unit) {
  321. case C.Y:
  322. result = getMonth() / 12;
  323. break;
  324. case C.M:
  325. result = getMonth();
  326. break;
  327. case C.Q:
  328. result = getMonth() / 3;
  329. break;
  330. case C.W:
  331. result = (diff - zoneDelta) / C.MILLISECONDS_A_WEEK;
  332. break;
  333. case C.D:
  334. result = (diff - zoneDelta) / C.MILLISECONDS_A_DAY;
  335. break;
  336. case C.H:
  337. result = diff / C.MILLISECONDS_A_HOUR;
  338. break;
  339. case C.MIN:
  340. result = diff / C.MILLISECONDS_A_MINUTE;
  341. break;
  342. case C.S:
  343. result = diff / C.MILLISECONDS_A_SECOND;
  344. break;
  345. default:
  346. result = diff; // milliseconds
  347. break;
  348. }
  349. return _float ? result : Utils.a(result);
  350. };
  351. _proto.daysInMonth = function daysInMonth() {
  352. return this.endOf(C.M).$D;
  353. };
  354. _proto.$locale = function $locale() {
  355. // get locale object
  356. return Ls[this.$L];
  357. };
  358. _proto.locale = function locale(preset, object) {
  359. if (!preset) return this.$L;
  360. var that = this.clone();
  361. var nextLocaleName = parseLocale(preset, object, true);
  362. if (nextLocaleName) that.$L = nextLocaleName;
  363. return that;
  364. };
  365. _proto.clone = function clone() {
  366. return Utils.w(this.$d, this);
  367. };
  368. _proto.toDate = function toDate() {
  369. return new Date(this.valueOf());
  370. };
  371. _proto.toJSON = function toJSON() {
  372. return this.isValid() ? this.toISOString() : null;
  373. };
  374. _proto.toISOString = function toISOString() {
  375. // ie 8 return
  376. // new Dayjs(this.valueOf() + this.$d.getTimezoneOffset() * 60000)
  377. // .format('YYYY-MM-DDTHH:mm:ss.SSS[Z]')
  378. return this.$d.toISOString();
  379. };
  380. _proto.toString = function toString() {
  381. return this.$d.toUTCString();
  382. };
  383. return Dayjs;
  384. }();
  385. var proto = Dayjs.prototype;
  386. dayjs.prototype = proto;
  387. [['$ms', C.MS], ['$s', C.S], ['$m', C.MIN], ['$H', C.H], ['$W', C.D], ['$M', C.M], ['$y', C.Y], ['$D', C.DATE]].forEach(function (g) {
  388. proto[g[1]] = function (input) {
  389. return this.$g(input, g[0], g[1]);
  390. };
  391. });
  392. dayjs.extend = function (plugin, option) {
  393. if (!plugin.$i) {
  394. // install plugin only once
  395. plugin(option, Dayjs, dayjs);
  396. plugin.$i = true;
  397. }
  398. return dayjs;
  399. };
  400. dayjs.locale = parseLocale;
  401. dayjs.isDayjs = isDayjs;
  402. dayjs.unix = function (timestamp) {
  403. return dayjs(timestamp * 1e3);
  404. };
  405. dayjs.en = Ls[L];
  406. dayjs.Ls = Ls;
  407. dayjs.p = {};
  408. export default dayjs;