date.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. export function getDate() {
  2. const date = new Date()
  3. const year = date.getFullYear()
  4. let month = date.getMonth() + 1
  5. let day = date.getDate()
  6. if (month < 10) {
  7. month = '0' + month
  8. }
  9. if (day < 10) {
  10. day = '0' + day
  11. }
  12. return year + '-' + month + '-' + day
  13. }
  14. export function formatDate(date) {
  15. const year = date.getFullYear()
  16. let month = date.getMonth() + 1
  17. let day = date.getDate()
  18. if (month < 10) {
  19. month = '0' + month
  20. }
  21. if (day < 10) {
  22. day = '0' + day
  23. }
  24. return year + '-' + month + '-' + day
  25. }
  26. export function getOneMonthOffset() {
  27. const myDate = new Date()
  28. let year = myDate.getFullYear()
  29. let month = myDate.getMonth()
  30. let date = myDate.getDate()
  31. let end = year + '-' + ('0' + (month + 1)).slice(-2) + '-' + ('0' + date).slice(-2)
  32. if (month === 0) {
  33. year -= 1
  34. month = 12
  35. } else if (month === 2) {
  36. date = date > 28 ? 28 : date
  37. } else {
  38. date = date > 30 ? 30 : date
  39. }
  40. let start = year + '-' + ('0' + month).slice(-2) + '-' + ('0' + date).slice(-2)
  41. return { start, end }
  42. }
  43. export function getOneWeekOffset(start) {
  44. const weekAgo = new Date(start).getTime() + 7 * 24 * 3600 * 1000
  45. const endDate = new Date(weekAgo)
  46. const year = endDate.getFullYear()
  47. const month = endDate.getMonth() + 1
  48. const date = endDate.getDate()
  49. const end = year + '-' + ('0' + month).slice(-2) + '-' + ('0' + date).slice(-2)
  50. return { start, end }
  51. }
  52. export function getHourMinute() {
  53. const date = new Date()
  54. const hour = date.getHours()
  55. const min = date.getMinutes()
  56. return Number(hour + '' + ('0' + min).slice(-2))
  57. }
  58. export function getTomorrow() {
  59. const tomorrow = new Date().getTime() + 24 * 3600 * 1000
  60. return new Date(tomorrow)
  61. }
  62. export function getAWeekFromNow() {
  63. const weekAfter = new Date().getTime() + 7 * 24 * 3600 * 1000
  64. return new Date(weekAfter)
  65. }
  66. export function getOneWeekText() {
  67. const day = new Date().getDay()
  68. const weeks = ['日', '一', '二', '三', '四', '五', '六']
  69. const arr = new Array(7)
  70. for (let index = 0; index < 7; index++) {
  71. let tempDay = (day + index) % 7
  72. arr[index] = weeks[tempDay]
  73. }
  74. return arr
  75. }
  76. export function getNextSevenDate() {
  77. const dates = new Array(7)
  78. for (let index = 0; index < dates.length; index++) {
  79. dates[index] = getDateByOffeset(index)
  80. }
  81. return dates
  82. }
  83. function getDateByOffeset(offset) {
  84. const date = new Date()
  85. date.setDate(date.getDate() + offset) //获取offset天后的日期
  86. const year = date.getFullYear()
  87. let month = date.getMonth() + 1 //获取当前月份
  88. month = month < 10 ? '0' + month : month
  89. let day = date.getDate() //获取当前月份的日期
  90. day = day < 10 ? '0' + day : day
  91. return {
  92. fullDate: year + '-' + month + '-' + day,
  93. date: day,
  94. }
  95. }
  96. export function formatDatetime(date) {
  97. const year = date.getFullYear()
  98. let month = date.getMonth() + 1
  99. let day = date.getDate()
  100. let hour = date.getHours()
  101. let minute = date.getMinutes()
  102. if (month < 10) {
  103. month = '0' + month
  104. }
  105. if (day < 10) {
  106. day = '0' + day
  107. }
  108. return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':00'
  109. }
  110. export function getDateTime() {
  111. const date = new Date()
  112. const year = date.getFullYear()
  113. let month = date.getMonth() + 1
  114. let day = date.getDate()
  115. let hour = date.getHours()
  116. let minute = date.getMinutes()
  117. let second = date.getSeconds()
  118. return year + '-' + ('0' + month).slice(-2) + '-' + ('0' + day).slice(-2) +
  119. ' ' + ('0' + hour).slice(-2) + ':' + ('0' + minute).slice(-2) + ':' + ('0' + second).slice(-2)
  120. }