date.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // 时间戳转换为年月日时分秒
  2. export const timesTampChange = (timestamp, format = 'Y-M-D h:m:s') => {
  3. if (!timestamp) return ''
  4. const date = new Date(timestamp)
  5. const Y = date.getFullYear().toString()
  6. const M = (date.getMonth() + 1).toString().padStart(2, '0')
  7. const D = date.getDate().toString().padStart(2, '0')
  8. const h = date.getHours().toString().padStart(2, '0')
  9. const m = date.getMinutes().toString().padStart(2, '0')
  10. const s = date.getSeconds().toString().padStart(2, '0')
  11. const formatter = { 'Y': Y, 'M': M, 'D': D, 'h': h, 'm': m, 's': s } // 替换format中的占位符
  12. let formattedDate = format.replace(/Y|M|D|h|m|s/g, matched => formatter[matched]) // 使用正则表达式匹配并替换占位符
  13. if (!formattedDate) formattedDate = Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
  14. return formattedDate
  15. }
  16. // 将 Wed May 01 2024 00:00:00 GMT+0800 (中国标准时间) 转换为时间戳
  17. export const getTimeStamp = (str) => {
  18. if (!str) return ''
  19. const date = new Date(str)
  20. return date.getTime()
  21. }
  22. // 传入一个时间戳返回这个日期的最早时间点以及最晚时间点 输出:[1721232000000, 1721318399999]
  23. export const getDayBounds = (timestamp) => {
  24. const date = new Date(timestamp)
  25. date.setHours(0, 0, 0, 0)
  26. const startOfDay = date.getTime()
  27. const endOfDay = new Date(timestamp)
  28. endOfDay.setHours(23, 59, 59, 999)
  29. if (endOfDay.getDate() !== date.getDate()) {
  30. endOfDay.setDate(endOfDay.getDate() - 1)
  31. endOfDay.setHours(23, 59, 59, 999)
  32. }
  33. // 返回包含最早和最晚时间点的时间戳的数组
  34. return [startOfDay, endOfDay.getTime()]
  35. }
  36. // 传入 Wed May 01 2024 00:00:00 GMT+0800 (中国标准时间) 输出 [2024-07-18 00:00:00, 2024-07-18 23:59:59]
  37. export const getStartAndEndOfDay = (dateString) => {
  38. const date = new Date(dateString + ' UTC')
  39. if (isNaN(date.getTime())) {
  40. throw new Error('Invalid date string')
  41. }
  42. const startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate())
  43. const endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
  44. function formatDate(dateObj) {
  45. let month = ('0' + (dateObj.getMonth() + 1)).slice(-2)
  46. let day = ('0' + dateObj.getDate()).slice(-2)
  47. let hours = ('0' + dateObj.getHours()).slice(-2)
  48. let minutes = ('0' + dateObj.getMinutes()).slice(-2)
  49. let seconds = ('0' + dateObj.getSeconds()).slice(-2)
  50. return dateObj.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  51. }
  52. return [formatDate(startDate), formatDate(endDate)]
  53. }
  54. // 传入一组时间戳,返回 [最早时间点,最晚时间点]
  55. export const convertTimestampsToDayRange = (timestamps) => {
  56. if (timestamps.length < 2) {
  57. throw new Error('Timestamps array must contain at least two elements')
  58. }
  59. function formatDate(date) {
  60. const year = date.getFullYear();
  61. const month = String(date.getMonth() + 1).padStart(2, '0')
  62. const day = String(date.getDate()).padStart(2, '0')
  63. const hours = String(date.getHours()).padStart(2, '0')
  64. const minutes = String(date.getMinutes()).padStart(2, '0')
  65. const seconds = String(date.getSeconds()).padStart(2, '0')
  66. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  67. }
  68. const startDate = new Date(timestamps[0])
  69. const endDate = new Date(timestamps[1])
  70. const startOfDay = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())
  71. const endOfDay = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate() + 1, 0, 0, 0, -1)
  72. return [formatDate(startOfDay), formatDate(endOfDay)]
  73. }