12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- // 时间戳转换为年月日时分秒
- export const timesTampChange = (timestamp) => {
- if (!timestamp) return ''
- const date = new Date(timestamp)
- const Y = date.getFullYear() + '-'
- const M = (date.getMonth() + 1).toString().padStart(2, '0') + '-'
- const D = date.getDate().toString().padStart(2, '0') + ' '
- const h = date.getHours().toString().padStart(2, '0') + ':'
- const m = date.getMinutes().toString().padStart(2, '0') + ':'
- const s = date.getSeconds().toString().padStart(2, '0')
- const strDate = Y + M + D + h + m + s
- return strDate
- }
- // 将 Wed May 01 2024 00:00:00 GMT+0800 (中国标准时间) 转换为时间戳
- export const getTimeStamp = (str) => {
- if (!str) return ''
- const date = new Date(str)
- return date.getTime()
- }
- // 传入一个时间戳返回这个日期的最早时间点以及最晚时间点 输出:[1721232000000, 1721318399999]
- export const getDayBounds = (timestamp) => {
- const date = new Date(timestamp)
- date.setHours(0, 0, 0, 0)
- const startOfDay = date.getTime()
- const endOfDay = new Date(timestamp)
- endOfDay.setHours(23, 59, 59, 999)
- if (endOfDay.getDate() !== date.getDate()) {
- endOfDay.setDate(endOfDay.getDate() - 1)
- endOfDay.setHours(23, 59, 59, 999)
- }
- // 返回包含最早和最晚时间点的时间戳的数组
- return [startOfDay, endOfDay.getTime()]
- }
- // 传入 Wed May 01 2024 00:00:00 GMT+0800 (中国标准时间) 输出 [2024-07-18 00:00:00, 2024-07-18 23:59:59]
- export const getStartAndEndOfDay = (dateString) => {
- const date = new Date(dateString + ' UTC')
- if (isNaN(date.getTime())) {
- throw new Error('Invalid date string')
- }
- const startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate())
- const endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
- function formatDate(dateObj) {
- let month = ('0' + (dateObj.getMonth() + 1)).slice(-2)
- let day = ('0' + dateObj.getDate()).slice(-2)
- let hours = ('0' + dateObj.getHours()).slice(-2)
- let minutes = ('0' + dateObj.getMinutes()).slice(-2)
- let seconds = ('0' + dateObj.getSeconds()).slice(-2)
- return dateObj.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
- }
- return [formatDate(startDate), formatDate(endDate)]
- }
|