date.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const Utils = {
  2. /**
  3. * 是否为闫年
  4. * @return {Boolse} true|false
  5. */
  6. isLeapYear: function(y: number): boolean {
  7. return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
  8. },
  9. /**
  10. * 返回星期数
  11. * @return {String}
  12. */
  13. getWhatDay: function(year: number, month: number, day: number): string {
  14. const date = new Date(year + '/' + month + '/' + day);
  15. const index = date.getDay();
  16. const dayNames = [
  17. '星期日',
  18. '星期一',
  19. '星期二',
  20. '星期三',
  21. '星期四',
  22. '星期五',
  23. '星期六'
  24. ];
  25. return dayNames[index];
  26. },
  27. /**
  28. * 返回星期数
  29. * @return {Number}
  30. */
  31. getMonthPreDay: function(year: number, month: number): number {
  32. const date = new Date(year + '/' + month + '/01');
  33. let day = date.getDay();
  34. if (day == 0) {
  35. day = 7;
  36. }
  37. return day;
  38. },
  39. /**
  40. * 返回月份天数
  41. * @return {Number}
  42. */
  43. getMonthDays: function(year: string, month: string): number {
  44. if (/^0/.test(month)) {
  45. month = month.split('')[1];
  46. }
  47. return ([
  48. 0,
  49. 31,
  50. this.isLeapYear(Number(year)) ? 29 : 28,
  51. 31,
  52. 30,
  53. 31,
  54. 30,
  55. 31,
  56. 31,
  57. 30,
  58. 31,
  59. 30,
  60. 31
  61. ] as number[])[month as any];
  62. },
  63. /**
  64. * 补齐数字位数
  65. * @return {string}
  66. */
  67. getNumTwoBit: function(n: number): string {
  68. n = Number(n);
  69. return (n > 9 ? '' : '0') + n;
  70. },
  71. /**
  72. * 日期对象转成字符串
  73. * @return {string}
  74. */
  75. date2Str: function(date: Date, split?: string): string {
  76. split = split || '-';
  77. const y = date.getFullYear();
  78. const m = this.getNumTwoBit(date.getMonth() + 1);
  79. const d = this.getNumTwoBit(date.getDate());
  80. return [y, m, d].join(split);
  81. },
  82. /**
  83. * 返回日期格式字符串
  84. * @param {Number} 0返回今天的日期、1返回明天的日期,2返回后天得日期,依次类推
  85. * @return {string} '2014-12-31'
  86. */
  87. getDay: function(i: number): string {
  88. i = i || 0;
  89. let date = new Date();
  90. const diff = i * (1000 * 60 * 60 * 24);
  91. date = new Date(date.getTime() + diff);
  92. return this.date2Str(date);
  93. },
  94. /**
  95. * 时间比较
  96. * @return {Boolean}
  97. */
  98. compareDate: function(date1: string, date2: string): boolean {
  99. const startTime = new Date(date1.replace('-', '/').replace('-', '/'));
  100. const endTime = new Date(date2.replace('-', '/').replace('-', '/'));
  101. if (startTime >= endTime) {
  102. return false;
  103. }
  104. return true;
  105. },
  106. /**
  107. * 时间是否相等
  108. * @return {Boolean}
  109. */
  110. isEqual: function(date1: string, date2: string): boolean {
  111. const startTime = new Date(date1).getTime();
  112. const endTime = new Date(date2).getTime();
  113. if (startTime == endTime) {
  114. return true;
  115. }
  116. return false;
  117. }
  118. };
  119. export default Utils;