多租户商城-商户小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.2 KiB

2 years ago
  1. import moment from 'moment'
  2. const DEFAULT_FORMAT = {
  3. date: 'YYYY-MM-DD HH:mm:ss'
  4. }
  5. const DATE_FORMAT = {
  6. date: 'YYYY-MM-DD'
  7. }
  8. const MONTH_FORMAT = {
  9. date: 'YYYY-MM'
  10. }
  11. let dateUtil = {}
  12. dateUtil = {
  13. format (time, format) {
  14. if (time !== null && time !== undefined) {
  15. if (format === null || format === undefined) {
  16. format = DEFAULT_FORMAT.date
  17. }
  18. return moment(time).format(format)
  19. }
  20. },
  21. formatDate (time) {
  22. if (time === null || time === undefined) {
  23. time = new Date()
  24. }
  25. return moment(time).format(DATE_FORMAT.date)
  26. },
  27. formatMonth(time) {
  28. if (time === null || time === undefined) {
  29. time = new Date()
  30. }
  31. return moment(time).format(MONTH_FORMAT.date)
  32. },
  33. timestamp (obj, format) {
  34. if (obj !== undefined && obj !== null) {
  35. return moment(obj, format).valueOf()
  36. }
  37. },
  38. date (obj, format) {
  39. if (obj !== undefined && obj !== null) {
  40. return moment(obj, format).toDate()
  41. }
  42. },
  43. addMinutes (time, minutes, format) {
  44. if (time !== undefined && time !== null) {
  45. return moment(time, format).add(minutes, 'm').valueOf()
  46. }
  47. },
  48. addHours (time, hours, format) {
  49. if (time !== undefined && time !== null) {
  50. return moment(time, format).add(hours, 'h').valueOf()
  51. }
  52. },
  53. addDays (time, days, format) {
  54. if (time !== undefined && time !== null) {
  55. return moment(time, format).add(days, 'd').valueOf()
  56. }
  57. },
  58. addWeeks (time, weeks, format) {
  59. if (time !== undefined && time !== null) {
  60. return moment(time, format).add(weeks, 'w').valueOf()
  61. }
  62. },
  63. addMonths (time, months, format) {
  64. if (time !== undefined && time !== null) {
  65. return moment(time, format).add(months, 'M').valueOf()
  66. }
  67. },
  68. addYears (time, years, format) {
  69. if (time !== undefined && time !== null) {
  70. return moment(time, format).add(years, 'y').valueOf()
  71. }
  72. },
  73. today () {
  74. return moment().startOf('day').valueOf()
  75. },
  76. now () {
  77. return new Date().getTime()
  78. },
  79. /**
  80. * 获取开始日期时间戳
  81. * 获取昨天的开始时间戳
  82. * this.$DateUtil.startOf('day', -1)
  83. * 获取月的开始时间戳
  84. * this.$DateUtil.startOf('Month')
  85. * @param {*} type day;week;Month;Year
  86. * @param {*} num 前后的差数
  87. * @returns
  88. */
  89. startOf (type, num) {
  90. if (num === undefined) {
  91. num = 0
  92. }
  93. return moment().add(num, type.substr(0, 1)).startOf(type).valueOf()
  94. },
  95. /**
  96. * 获取结束日期时间戳
  97. * 获取昨天的开始时间戳
  98. * this.$DateUtil.endOf('day', -1)
  99. * 获取月的结束时间戳
  100. * this.$DateUtil.endOf('Month')
  101. * @param {*} type day;week;Month;Year
  102. * @param {*} num 前后的差数
  103. * @returns
  104. */
  105. endOf (type, num) {
  106. if (num === undefined) {
  107. num = 0
  108. }
  109. return moment().add(num, type.substr(0, 1)).endOf(type).valueOf()
  110. }
  111. }
  112. export default dateUtil