date.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {stringIsBlank} from "@/utils/blank-utils";
  2. import moment from "moment";
  3. export function getDate() {
  4. const date = new Date()
  5. const year = date.getFullYear()
  6. let month = date.getMonth() + 1
  7. let day = date.getDate()
  8. return year + '-' + ('0' + month).slice(-2) + '-' + ('0' + day).slice(-2)
  9. }
  10. export function getDatetime() {
  11. const now = new Date()
  12. const year = now.getFullYear()
  13. const month = now.getMonth() + 1
  14. const day = now.getDate()
  15. const hh = now.getHours()
  16. const mm = now.getMinutes()
  17. const ss = now.getSeconds()
  18. let clock = year + '-'
  19. if (month < 10) clock += '0'
  20. clock += month + '-'
  21. if (day < 10) clock += '0'
  22. clock += day + ' '
  23. if (hh < 10) clock += '0'
  24. clock += hh + ':'
  25. if (mm < 10) clock += '0'
  26. clock += mm + ':'
  27. if (ss < 10) clock += '0'
  28. clock += ss
  29. return clock
  30. }
  31. export function compareDate(date1, date2) {
  32. const oDate1 = new Date(date1);
  33. const oDate2 = new Date(date2);
  34. return oDate1.getTime() > oDate2.getTime();
  35. }
  36. export function huanHangXianShi(date) {
  37. let clock = date.split(" ")
  38. return clock[0] + '<br />' + clock[1]
  39. }
  40. export function formatDatetime(date) {
  41. if (typeof date === 'undefined') return null
  42. if (date === '' || date === null) return null
  43. if (typeof date === 'string') return date
  44. const year = date.getFullYear()
  45. const month = date.getMonth() + 1
  46. const day = date.getDate()
  47. const hh = date.getHours()
  48. const mm = date.getMinutes()
  49. const ss = date.getSeconds()
  50. let clock = year + '-'
  51. if (month < 10) clock += '0'
  52. clock += month + '-'
  53. if (day < 10) clock += '0'
  54. clock += day + ' '
  55. if (hh < 10) clock += '0'
  56. clock += hh + ':'
  57. if (mm < 10) clock += '0'
  58. clock += mm + ':'
  59. if (ss < 10) clock += '0'
  60. clock += ss
  61. return clock
  62. }
  63. export function formatDate(date) {
  64. if (typeof date === 'undefined') return null
  65. if (date === '' || date === null) return null
  66. if (typeof date === 'string') return date
  67. const year = date.getFullYear()
  68. const month = date.getMonth() + 1
  69. const day = date.getDate()
  70. return year + '-' + ('0' + month).slice(-2) + '-' + ('0' + day).slice(-2)
  71. }
  72. export function getOneMonthOffset() {
  73. const myDate = new Date()
  74. let year = myDate.getFullYear()
  75. let month = myDate.getMonth()
  76. let date = myDate.getDate()
  77. let end = year + '-' + ('0' + (month + 1)).slice(-2) + '-' + ('0' + date).slice(-2)
  78. if (month === 0) {
  79. year -= 1
  80. month = 12
  81. } else if (month === 2) {
  82. date = date > 28 ? 28 : date
  83. } else {
  84. date = date > 30 ? 30 : date
  85. }
  86. let start = year + '-' + ('0' + month).slice(-2) + '-' + ('0' + date).slice(-2)
  87. return {start, end}
  88. }
  89. export function getDateRangeFormatDate(data) {
  90. let startTime = ''
  91. let endTime = ''
  92. //用户手动输入了日期 并且没有按下大键盘上面的回车
  93. if (data === null || data.length === 0) return {startTime, endTime}
  94. if (typeof data[0].$d !== 'undefined') {
  95. startTime = formatDate(data[0].$d) + ' 00:00:00'
  96. endTime = formatDate(data[1].$d) + ' 23:59:59'
  97. } else if (data !== null) {
  98. startTime = formatDate(data[0]) + ' 00:00:00'
  99. endTime = formatDate(data[1]) + ' 23:59:59'
  100. }
  101. return {startTime, endTime}
  102. }
  103. export function getDateRangeFormatDateTime(date) {
  104. let startTime = ''
  105. let endTime = ''
  106. if (!date) return {startTime, endTime}
  107. if (!date[0] || !date[1]) return {startTime, endTime}
  108. //用户手动输入了日期 并且没有按下大键盘上面的回车
  109. if (!date[0].$d) {
  110. startTime = formatDatetime(date[0].$d)
  111. endTime = formatDatetime(date[1].$d)
  112. } else {
  113. startTime = formatDatetime(date[0])
  114. endTime = formatDatetime(date[1])
  115. }
  116. return {startTime, endTime}
  117. }
  118. export function formatMonth(date) {
  119. if (typeof date === 'undefined') return null
  120. if (date === '' || date === null) return null
  121. if (typeof date === 'string') return date
  122. const year = date.getFullYear()
  123. const month = date.getMonth() + 1
  124. return year + '-' + ('0' + month).slice(-2)
  125. }
  126. /**
  127. * 格式化日期
  128. * @param date 日期
  129. * @param pattern 格式
  130. * @returns {string} 返回格式好的日期
  131. */
  132. export function getFormatDatetime(date, pattern) {
  133. if (stringIsBlank(pattern)) {
  134. pattern = "YYYY-MM-DD HH:mm:ss"
  135. }
  136. if (stringIsBlank(date)) {
  137. return ""
  138. }
  139. return moment(date).format(pattern)
  140. }