项目上用到很多时间格式,写了一些处理时间的方法,也可以用第三方库,下面是我自己项目封装的一下方法,命名瞎写的,有些方法是关联的,很多没有怎么完善,记录一下,兼容ios时间问题,时间太赶没有完善,将就用下,人和程序有一个人跑就行
获取当前时间戳 getTodayTomorrowTtem
export const getTodayTomorrowTtem = () => {
return store.getters.serverTime || new Date().getTime()
}
YY-MM-DD 转时间戳 如果已经是时间戳则不操作 getTimeStamp
export const getTimeStamp = date => {
if (!date) return ''
if (typeof date === 'string') {
date = date.replace(/-/g, '/')
}
return Date.parse(new Date(date))
}
时间戳 转 YY-MM-DD getDateFormats
export const getDateFormats = (date, format = 'YY-MM-DD') => {
if (!date) return false
if (typeof date === 'string') {
date = date.replace(/-/g, '/')
}
return getDateFormat(new Date(date), format)
}
中国标准时间 转 YY-MM-DD getDateFormat
- 示例:Wed Jan 04 2023 16:17:30 GMT+0800 (中国标准时间)
export const getDateFormat = (date, format = 'YY-MM-DD') => {
let YY = date.getFullYear() + '-'
let month = date.getMonth() + 1
let day = date.getDate()
let MM = month < 10 ? '0' + month + '-' : month + '-'
let DD = day < 10 ? '0' + day : day + ''
if (format === 'YY-MM-DD') return YY + MM + DD
if (format === 'MM-DD') return MM + DD
if (format === 'DD') return DD
}
转换时间格式 dateFormats
- 示例: 2022-12-12 => 2022.12.12 或 2022-12-12 12:12:59 => 2022.12.12 12:12:59
export const dateFormats = item => {
if (!item) return
let date = item.split(' ')
let createTime = date[0].replace(/-/g, '/')
let arr = createTime.split('/')
let i = ''
if (date[1]) {
i = ' ' + date[1]
}
return arr[0] + '.' + parseInt(Number(arr[1])) + '.' + parseInt(Number(arr[2])) + i
}
2022-12-12 转年月日多格式 getYearMonthDay
export const getYearMonthDay = (date, format) => {
if (!date) return false
if (typeof date === 'string') {
date = date.replace(/-/g, '/')
}
return getSpecificDate(new Date(date), format)
}
中国标准时间 转年月日多格式 getSpecificDate
export const getSpecificDate = (date, format = 'YY-MM-DD') => {
let YY = date.getFullYear() + '年'
let MM = date.getMonth() + 1 + '月'
let DD = date.getDate() + '日'
if (format === 'YY-MM-DD') return YY + MM + DD
if (format === 'MM-DD') return MM + DD
if (format === 'DD') return DD
if (format === 'HH:mm') return DD
if (format === 'YY.MM.DD')
return date.getFullYear() + '.' + (date.getMonth() + 1) + '.' + date.getDate()
}
处理时间 handleTime
- 示例: 2023-01-04 = 周三
export const handleTime = item => {
if (!item) return
let createTime = item.replace(/-/g, '/')
return week[new Date(createTime).getDay()]
}
中国标准时间 获取星期几 getWeek
const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
export const getWeek = date => {
return week[date.getDay()]
}
检测活动是否开始 checkTimeStatus
// 检测状态 0 还未开始 1 活动期间 2 结束
export const checkTimeStatus = function(beginTime, endTime, systemTime) {
let start = getTimeStamp(beginTime) //开始时间
let end = getTimeStamp(endTime) //结束时间
let now = getTimeStamp(systemTime || getTodayTomorrowTtem()) //不传默认取当前时间
// 不配置
if (!start && !end) {
return 1
}
// 开始日期无
if (!start && end) {
return now <= end ? 1 : 2
}
// 结束日期无
if (start && !end) {
return now >= start ? 1 : 0
}
if (now < start) {
// 未开始
return 0
} else if (now > end) {
// 结束
return 2
} else {
// 活动中
return 1
}
}
计算剩余小时和分钟 getTimeRemainings
export const getTimeRemainings = (item, num = 30) => {
if (!item) return ''
let date = getTimeStamp(item) + 60 * num * 1000 - getTodayTomorrowTtem()
if (date < 0) return '1分钟'
let H = parseInt(date / 1000 / 60 / 60) // 小时
let H1 = date % (1000 * 60 * 60)
let minute = parseInt(H1 / 1000 / 60) // 剩余分钟
return H > 0 ? H + '小时' + minute + '分钟' : minute + '分钟'
}
计算剩余分钟 getTimeRemaining
export const getTimeRemaining = (item, text = true, num = 30) => {
if (!item) return ''
let date = getTimeStamp(item) + 60 * num * 1000 - getTodayTomorrowTtem()
if (date < 0) return text ? '1分钟' : 0
let minute = Math.ceil(date / 1000 / 60)
return (minute > num ? num : minute) + (text ? '分钟' : 0)
}
计算开始时间和结束时间相隔几个晚上 getCalculationDate
export const getCalculationDate = (startDate, endDate) => {
let dete = getTimeStamp(endDate) - getTimeStamp(startDate)
return Math.trunc(dete / 1000 / 60 / 60 / 24)
}
日期取整 deleteDate
- 2022-06-06 12:12:36 => 2022-06-06
export const deleteDate = date => {
return date.split(' ')[0]
}
活动倒计时 deleteDate
// 格式化时间
export const formatTime = (seconds, showDays, valueFun) => {
// 小于等于0的话,结束倒计时
seconds <= 0 && end()
let [day, hour, minute, second] = [0, 0, 0, 0]
day = parseInt(seconds / (60 * 60 * 24 * 1000))
hour = parseInt((seconds - day * 60 * 60 * 24 * 1000) / (60 * 60 * 1000))
let showHour = null
if (showDays) {
showHour = hour
} else {
// 如果不显示天数,将“天”部分的时间折算到小时中去
showHour = parseInt(seconds / (60 * 60 * 1000))
}
minute = parseInt((seconds - day * 60 * 60 * 24 * 1000 - hour * 60 * 60 * 1000) / (60 * 1000)) // 剩余分钟
second = parseInt(
(seconds - day * 60 * 60 * 24 * 1000 - hour * 60 * 60 * 1000 - minute * 60 * 1000) / 1000
)
// 如果小于10,在前面补上一个"0"
showHour = showHour < 10 ? '0' + showHour : showHour
minute = minute < 10 ? '0' + minute : minute
second = second < 10 ? '0' + second : second
day = day < 10 ? '0' + day : day
valueFun([day, showHour, minute, second])
}
var timer = null
// 倒计时
export const start = query => {
let { endTime, showDays = true, valueFun } = query
// 避免可能出现的倒计时重叠情况
let date = getTimeStamp(endTime) - getTodayTomorrowTtem()
clearTimer()
console.log(date, getTimeStamp(endTime), getTodayTomorrowTtem())
if (date <= 0) {
valueFun(['00', '00', '00', '00'])
return
}
date = Number(date)
formatTime(date, showDays, valueFun)
timer = setInterval(() => {
date -= 1000
// 发出change事件
if (date <= 0) {
clearInterval(timer)
return end()
}
formatTime(date, showDays, valueFun)
}, 1000)
}
// 停止倒计时
export const end = () => {
clearTimer()
}
// 清除定时器
export const clearTimer = () => {
console.log('清除定时器', timer)
if (timer) {
// 清除定时器
clearInterval(timer)
timer = null
}
}
小白一个,学不动就回家养猪