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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
| class Dayjs { constructor(cfg) { this.$L = parseLocale(cfg.locale, null, true); this.parse(cfg); }
parse(cfg) { this.$d = parseDate(cfg); this.$x = cfg.x || {}; this.init(); }
init() { const { $d } = this; this.$y = $d.getFullYear(); this.$M = $d.getMonth(); this.$D = $d.getDate(); this.$W = $d.getDay(); this.$H = $d.getHours(); this.$m = $d.getMinutes(); this.$s = $d.getSeconds(); this.$ms = $d.getMilliseconds(); }
$utils() { return Utils; }
isValid() { return !(this.$d.toString() === C.INVALID_DATE_STRING); }
isSame(that, units) { const other = dayjs(that); return this.startOf(units) <= other && other <= this.endOf(units); }
isAfter(that, units) { return dayjs(that) < this.startOf(units); }
isBefore(that, units) { return this.endOf(units) < dayjs(that); }
$g(input, get, set) { if (Utils.u(input)) return this[get]; return this.set(set, input); }
/** * @description: 返回秒做单位的 Unix 时间戳 (10 位数字) * @return {Number} 十位时间戳 */ unix() { return Math.floor(this.valueOf() / 1000); }
valueOf() { return this.$d.getTime(); }
startOf(units, startOf) { const isStartOf = !Utils.u(startOf) ? startOf : true; const unit = Utils.p(units);
const instanceFactory = (d, m) => { const ins = Utils.w( this.$u ? Date.UTC(this.$y, m, d) : new Date(this.$y, m, d), this ); return isStartOf ? ins : ins.endOf(C.D); };
const instanceFactorySet = (method, slice) => { const argumentStart = [0, 0, 0, 0]; const argumentEnd = [23, 59, 59, 999]; return Utils.w( this.toDate()[method].apply( this.toDate('s'), (isStartOf ? argumentStart : argumentEnd).slice(slice) ), this ); };
const { $W, $M, $D } = this; const utcPad = `set${this.$u ? 'UTC' : ''}`; switch (unit) { case C.Y: return isStartOf ? instanceFactory(1, 0) : instanceFactory(31, 11); case C.M: return isStartOf ? instanceFactory(1, $M) : instanceFactory(0, $M + 1); case C.W: { const weekStart = this.$locale().weekStart || 0; const gap = ($W < weekStart ? $W + 7 : $W) - weekStart; return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M); } case C.D: case C.DATE: return instanceFactorySet(`${utcPad}Hours`, 0); case C.H: return instanceFactorySet(`${utcPad}Minutes`, 1); case C.MIN: return instanceFactorySet(`${utcPad}Seconds`, 2); case C.S: return instanceFactorySet(`${utcPad}Milliseconds`, 3); default: return this.clone(); } }
endOf(arg) { return this.startOf(arg, false); }
$set(units, int) { const unit = Utils.p(units); const utcPad = `set${this.$u ? 'UTC' : ''}`; const name = { [C.D]: `${utcPad}Date`, [C.DATE]: `${utcPad}Date`, [C.M]: `${utcPad}Month`, [C.Y]: `${utcPad}FullYear`, [C.H]: `${utcPad}Hours`, [C.MIN]: `${utcPad}Minutes`, [C.S]: `${utcPad}Seconds`, [C.MS]: `${utcPad}Milliseconds`, }[unit]; const arg = unit === C.D ? this.$D + (int - this.$W) : int;
if (unit === C.M || unit === C.Y) { const date = this.clone().set(C.DATE, 1); date.$d[name](arg); date.init(); this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).$d; } else if (name) this.$d[name](arg);
this.init(); return this; }
set(string, int) { return this.clone().$set(string, int); }
/** * @description: 从实例中获取相应信息的通用 getter。 * @param {String} unit * @return {Number} 返回对应单位的值 */ get(unit) { return this[Utils.p(unit)](); }
/** * @description: 根据单位和值,给当前关联的Date对象增加时间 * @param {Number} number 增加的数值 * @param {String} units 单位 * @return {Dayjs} 返回新的Dayjs对象 */ add(number, units) { number = Number(number); // eslint-disable-line no-param-reassign const unit = Utils.p(units); /** * @description: 专门给 week 和 date 用的增加时间的工具函数 * @param {Number} n 基础单位的天数 例如 week 是 7 * @return {Dayjs} 返回新的 Dayjs 对象 */ const instanceFactorySet = (n) => { const d = dayjs(this); return Utils.w(d.date(d.date() + Math.round(n * number)), this); }; // 月 if (unit === C.M) { return this.set(C.M, this.$M + number); } // 年 if (unit === C.Y) { return this.set(C.Y, this.$y + number); } // 日 if (unit === C.D) { return instanceFactorySet(1); } // 周 if (unit === C.W) { return instanceFactorySet(7); } const step = { [C.MIN]: C.MILLISECONDS_A_MINUTE, // 分钟 [C.H]: C.MILLISECONDS_A_HOUR, // 小时 [C.S]: C.MILLISECONDS_A_SECOND, // 秒 }[unit] || 1; // ms
const nextTimeStamp = this.$d.getTime() + number * step; return Utils.w(nextTimeStamp, this); }
/** * @description: 根据单位和值,给当前关联的Date对象减少时间 * @param {Number} number 减少的数值 * @param {String} units 单位 * @return {Dayjs} 返回新的Dayjs对象 */ subtract(number, string) { return this.add(number * -1, string); }
/** * @description: 根据模板返回对应格式的时间字符串 * @param {String} formatStr 模板字符串 * @return {String} 对应格式的时间字符串 */ format(formatStr) { if (!this.isValid()) return C.INVALID_DATE_STRING;
// 整理出所需的各种变量和方法 const str = formatStr || C.FORMAT_DEFAULT; const zoneStr = Utils.z(this); const locale = this.$locale(); const { $H, $m, $M } = this; const { weekdays, months, meridiem } = locale;
/** * @description: 返回对应缩写的字符串,可自适应 * @param {Array} arr 几月或者周几的缩写数组 ["1月", "2月", "3月"...] * @param {Number} index 索引 * @param {Array} full 几月或者周几的非缩写数组 ["一月", "二月", "三月"...] * @param {Number} length 返回结果的字符数 * @return {String} 对应缩写的字符串 */ const getShort = (arr, index, full, length) => (arr && (arr[index] || arr(this, str))) || full[index].substr(0, length);
/** * @description: 获取固定长度的小时表示 * @param {Number} num 小时的长度 * @return {String} 固定长度的小时表示 */ const get$H = (num) => Utils.s($H % 12 || 12, num, '0');
/** * @description: 根据时和分区分时间段(上午、下午) * @param {Number} hour 时 * @param {Number} minute 分 * @param {Boolean} isLowercase 是否小写,默认false * @return {String} 时间段 例如 AM */ const meridiemFunc = meridiem || ((hour, minute, isLowercase) => { const m = hour < 12 ? 'AM' : 'PM'; return isLowercase ? m.toLowerCase() : m; });
// 不同的模板对应的格式转换 const matches = { YY: String(this.$y).slice(-2), YYYY: this.$y, M: $M + 1, MM: Utils.s($M + 1, 2, '0'), MMM: getShort(locale.monthsShort, $M, months, 3), MMMM: getShort(months, $M), D: this.$D, DD: Utils.s(this.$D, 2, '0'), d: String(this.$W), dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2), ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3), dddd: weekdays[this.$W], H: String($H), HH: Utils.s($H, 2, '0'), h: get$H(1), hh: get$H(2), a: meridiemFunc($H, $m, true), A: meridiemFunc($H, $m, false), m: String($m), mm: Utils.s($m, 2, '0'), s: String(this.$s), ss: Utils.s(this.$s, 2, '0'), SSS: Utils.s(this.$ms, 3, '0'), Z: zoneStr, // 'ZZ' logic below };
// /\[([^\]]+)]|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 return str.replace( C.REGEX_FORMAT, (match, $1) => $1 || matches[match] || zoneStr.replace(':', '') ); }
utcOffset() { return -Math.round(this.$d.getTimezoneOffset() / 15) * 15; }
diff(input, units, float) { const unit = Utils.p(units); const that = dayjs(input); const zoneDelta = (that.utcOffset() - this.utcOffset()) * C.MILLISECONDS_A_MINUTE; const diff = this - that; let result = Utils.m(this, that);
result = { [C.Y]: result / 12, [C.M]: result, [C.Q]: result / 3, [C.W]: (diff - zoneDelta) / C.MILLISECONDS_A_WEEK, [C.D]: (diff - zoneDelta) / C.MILLISECONDS_A_DAY, [C.H]: diff / C.MILLISECONDS_A_HOUR, [C.MIN]: diff / C.MILLISECONDS_A_MINUTE, [C.S]: diff / C.MILLISECONDS_A_SECOND, }[unit] || diff;
return float ? result : Utils.a(result); }
daysInMonth() { return this.endOf(C.M).$D; }
$locale() { return Ls[this.$L]; }
locale(preset, object) { if (!preset) return this.$L; const that = this.clone(); const nextLocaleName = parseLocale(preset, object, true); if (nextLocaleName) that.$L = nextLocaleName; return that; }
clone() { return Utils.w(this.$d, this); }
toDate() { return new Date(this.valueOf()); }
toJSON() { return this.isValid() ? this.toISOString() : null; }
toISOString() { return this.$d.toISOString(); }
toString() { return this.$d.toUTCString(); } }
|