date.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import moment from 'moment'
  2. const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
  3. const DATE_FORMAT = 'YYYY-MM-DD'
  4. const MONTH_FORMAT = 'YYYY-MM'
  5. const YEAR_FORMAT = 'YYYY'
  6. const TIME_FORMAT = 'HH:mm:ss'
  7. export default {
  8. parseDay(date) {
  9. if (date) {
  10. return date.format(DATE_TIME_FORMAT)
  11. }
  12. return date.format(DATE_TIME_FORMAT)
  13. },
  14. date(date, format) {
  15. if (date) {
  16. return moment(date).format(format || DATE_TIME_FORMAT)
  17. }
  18. return moment().format(format || DATE_TIME_FORMAT)
  19. },
  20. day(date) {
  21. if (date) {
  22. return moment(date).format(DATE_FORMAT)
  23. }
  24. return moment().format(DATE_FORMAT)
  25. },
  26. month(date) {
  27. if (date) {
  28. return moment(date).format(MONTH_FORMAT)
  29. }
  30. return moment().format(MONTH_FORMAT)
  31. },
  32. year(date) {
  33. if (date) {
  34. return moment(date).format(YEAR_FORMAT)
  35. }
  36. return moment().format(YEAR_FORMAT)
  37. },
  38. beforeDay(date, num = 1) {
  39. if (date) {
  40. return moment(date).subtract(num, 'days').format(DATE_FORMAT)
  41. }
  42. return moment().subtract(num, 'days').format(DATE_FORMAT)
  43. },
  44. afterDay(date, num = 1) {
  45. if (date) {
  46. return moment(date).add(num, 'days').format(DATE_FORMAT)
  47. }
  48. return moment().add(num, 'days').format(DATE_FORMAT)
  49. },
  50. time(date) {
  51. if (date) {
  52. return moment(date).format(TIME_FORMAT)
  53. }
  54. return moment().format(TIME_FORMAT)
  55. },
  56. unix(date) {
  57. if (date) {
  58. return moment(date).unix()
  59. }
  60. return moment().unix()
  61. },
  62. dayDiff(dateStart, dateEnd) {
  63. return moment(dateEnd).diff(moment(dateStart), 'day')
  64. },
  65. minuteDiff(startDate, endDate, type = 'minute') {
  66. return moment(endDate).diff(moment(startDate), type)
  67. },
  68. getMinutes(date) {
  69. let time = moment(date)
  70. return time.hours() * 60 + time.minutes()
  71. },
  72. beforeMonth(date, num = 1) {
  73. if (date) {
  74. return moment(date).subtract(num, 'months').format(MONTH_FORMAT)
  75. }
  76. return moment().subtract(num, 'months').format(MONTH_FORMAT)
  77. },
  78. afterMonth(date, num = 1) {
  79. if (date) {
  80. return moment(date).add(num, 'months').format(MONTH_FORMAT)
  81. }
  82. return moment().add(num, 'months').format(MONTH_FORMAT)
  83. },
  84. lastMonth(date) {
  85. return this.beforeMonth(date)
  86. },
  87. getMonthDays(date) {
  88. return moment(date).daysInMonth();
  89. },
  90. lastDayOfMonth(date) {
  91. if (date) {
  92. return moment(date).endOf('month').format(DATE_FORMAT)
  93. }
  94. return moment().endOf('month').format(DATE_FORMAT)
  95. },
  96. afterWeek(date, num = 1) {
  97. if (date) {
  98. return moment(date).add(num, 'weeks').format(DATE_FORMAT)
  99. }
  100. return moment().add(num, 'weeks').format(DATE_FORMAT)
  101. },
  102. afterTime(date, minutes = 1) {
  103. if (date) {
  104. return moment(date).add(minutes, 'minutes').format(DATE_TIME_FORMAT)
  105. }
  106. return moment().add(minutes, 'minutes').format(DATE_TIME_FORMAT)
  107. },
  108. firstDayOfNextWeek(date) {
  109. if (date) {
  110. return moment(date).add(1, 'weeks').startOf('week').format(DATE_FORMAT)
  111. }
  112. return moment(date).add(1, 'weeks').startOf('week').format(DATE_FORMAT)
  113. },
  114. getFeedBackDate(dateRange, day) {
  115. let startYear = moment(dateRange[0]).year()
  116. let endYear = moment(dateRange[1]).year()
  117. if (startYear == endYear) {
  118. return startYear + '-' + day
  119. }
  120. if (this.unix(startYear + '-' + day) > this.unix(dateRange[1])) {
  121. return startYear + '-' + day
  122. }
  123. return endYear + '-' + day
  124. }
  125. }