date.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 getRawMonthOffset() {
  90. const myDate = new Date()
  91. let year = myDate.getFullYear()
  92. let month = myDate.getMonth()
  93. let end = year + '-' + ('0' + (month + 1)).slice(-2)
  94. if (month === 0) {
  95. year -= 1
  96. month = 12
  97. }
  98. let start = year + '-' + ('0' + month).slice(-2)
  99. return { start, end }
  100. }
  101. export function getDateRangeFormatDate(data) {
  102. let startTime = ''
  103. let endTime = ''
  104. //用户手动输入了日期 并且没有按下大键盘上面的回车
  105. if (data === null || data.length === 0) return { startTime, endTime }
  106. if (typeof data[0].$d !== 'undefined') {
  107. startTime = formatDate(data[0].$d) + ' 00:00:00'
  108. endTime = formatDate(data[1].$d) + ' 23:59:59'
  109. } else {
  110. startTime = formatDate(data[0]) + ' 00:00:00'
  111. endTime = formatDate(data[1]) + ' 23:59:59'
  112. }
  113. return { startTime, endTime }
  114. }
  115. export function getDateRangeFormatDateTime(date) {
  116. let startTime = ''
  117. let endTime = ''
  118. if (!date) return { startTime, endTime }
  119. if (!date[0] || !date[1]) return { startTime, endTime }
  120. //用户手动输入了日期 并且没有按下大键盘上面的回车
  121. if (date[0].$d) {
  122. startTime = getFormatDatetime(date[0].$d)
  123. endTime = getFormatDatetime(date[1].$d)
  124. } else {
  125. startTime = getFormatDatetime(date[0])
  126. endTime = getFormatDatetime(date[1])
  127. }
  128. return { startTime, endTime }
  129. }
  130. export function formatYear(date) {
  131. if (typeof date === 'undefined') return null
  132. if (date === '' || date === null) return null
  133. if (typeof date === 'string') return date
  134. return date.getFullYear()
  135. }
  136. export function formatMonth(date) {
  137. if (typeof date === 'undefined') return null
  138. if (date === '' || date === null) return null
  139. if (typeof date === 'string') return date
  140. const year = date.getFullYear()
  141. const month = date.getMonth() + 1
  142. return year + '-' + ('0' + month).slice(-2)
  143. }
  144. /**
  145. * 格式化日期
  146. * @param date 日期
  147. * @param pattern 格式
  148. * @returns {string} 返回格式好的日期
  149. */
  150. export function getFormatDatetime(date, pattern) {
  151. if (stringIsBlank(pattern)) {
  152. pattern = 'YYYY-MM-DD HH:mm:ss'
  153. }
  154. if (stringIsBlank(date)) {
  155. return ''
  156. }
  157. return moment(date).format(pattern)
  158. }
  159. export function getLastMonth() {
  160. const myDate = new Date()
  161. let year = myDate.getFullYear()
  162. let month = myDate.getMonth()
  163. if (month === 0) {
  164. year -= 1
  165. month = 12
  166. }
  167. return year + '-' + ('0' + month).slice(-2)
  168. }