date.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 === 'string') return date
  138. if (typeof date === 'undefined') return null
  139. if (date === '' || date === null) return null
  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 = 'YYYY-MM-DD HH:mm:ss') {
  151. if (stringIsBlank(date)) {
  152. return ''
  153. }
  154. return moment(date).format(pattern)
  155. }
  156. export function getLastMonth() {
  157. const myDate = new Date()
  158. let year = myDate.getFullYear()
  159. let month = myDate.getMonth()
  160. if (month === 0) {
  161. year -= 1
  162. month = 12
  163. }
  164. return year + '-' + ('0' + month).slice(-2)
  165. }
  166. export function judgeToday(date1, date2) {
  167. return moment(date2).isSame(moment(), "day")
  168. }
  169. // 时间相减
  170. export function subtractTime(date1, date2) {
  171. date1 = moment(date1)
  172. date2 = moment(date2)
  173. return date1.diff(date2, 'day', false)
  174. }
  175. export function formatMonth1(date) {
  176. if (typeof date === 'undefined') return null
  177. if (date === '' || date === null) return null
  178. if (typeof date === 'string') return date
  179. const year = date.getFullYear()
  180. const month = date.getMonth() + 1
  181. return year + ('0' + month).slice(-2)
  182. }
  183. export function getDateTiffDays(value) {
  184. // value---最近天数
  185. return moment(new Date().getTime() - value * 1000 * 24 * 60 * 60).format('YYYY-MM-DD hh:mm:ss')
  186. }
  187. // 获取前day的日期(arr是日期拼接符,默认'-')
  188. export function getDayAgo(day, arr, type) {
  189. var today = new Date();
  190. // 日期拼接符
  191. if (!arr) {
  192. arr = '-'
  193. }
  194. var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  195. //注意,这行是关键代码
  196. today.setTime(targetday_milliseconds);
  197. var tYear = today.getFullYear();
  198. var tMonth = today.getMonth();
  199. var tDate = today.getDate();
  200. tMonth = doHandleMonth(tMonth + 1);
  201. tDate = doHandleMonth(tDate);
  202. // 显示年月
  203. if (1 === type) {
  204. return tYear + arr + tMonth
  205. } else if (2 === type) {
  206. // 显示月日
  207. return tMonth + arr + tDate;
  208. } else {
  209. // 显示年月日
  210. return tYear + arr + tMonth + arr + tDate;
  211. }
  212. }
  213. function doHandleMonth(month) {
  214. var m = month;
  215. if (month.toString().length == 1) {
  216. m = "0" + month;
  217. }
  218. return m;
  219. }