ReportDate.ts 929 B

12345678910111213141516171819202122232425262728293031323334
  1. export class ReportDate extends Date {
  2. // prototype: any;
  3. // constructor() {
  4. // super();
  5. // set(this.prototype, 'Format', this.Format);
  6. // Object.assign(Date, this);
  7. // }
  8. Format = (fmt: string) => {
  9. const o: Record<string, any> = {
  10. 'M+': this.getMonth() + 1, // 月份
  11. 'd+': this.getDate(), // 日
  12. 'h+': this.getHours(), // 小时
  13. 'm+': this.getMinutes(), // 分
  14. 's+': this.getSeconds(), // 秒
  15. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  16. S: this.getMilliseconds(), // 毫秒
  17. };
  18. if (/(y+)/.test(fmt))
  19. fmt = fmt.replace(
  20. RegExp.$1,
  21. `${this.getFullYear()}`.substr(4 - RegExp.$1.length)
  22. );
  23. for (const k in o)
  24. if (new RegExp(`(${k})`).test(fmt))
  25. fmt = fmt.replace(
  26. RegExp.$1,
  27. RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
  28. );
  29. return fmt;
  30. };
  31. }