目录
1、格式化后的日期时间:2023/06/05 16:47:24
1、格式化后的日期时间:2023/06/05 16:47:24
const formatTime = dateE => {
let date = ''
if (dateE) {
date = dateE
} else {
date = new Date()
}
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
2、格式化后的日期:2023-6-5
const formatDate = dateE => {
let date = ''
if (dateE) {
date = dateE
} else {
date = new Date()
}
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
return `${year}-${month}-${day}`
}
3、格式化获取当月的第一天:2023-6-1
const formatMonthStart = dateE => {
let date = ''
if (dateE) {
date = dateE
} else {
date = new Date()
}
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = 1
return `${year}-${month}-${day}`
}
4、格式化获取当月的最后一天:2023-6-30
const formatMonthEnd = dateE => {
let date = ''
if (dateE) {
date = dateE
} else {
date = new Date()
}
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = new Date(year, month, 0).getDate()
return `${year}-${month}-${day}`
}
5、格式化传入的日期得出当月的第一天:2023-6-1
const formatParamsStart = (date) => {
const year = date.split('-')[0]
const month = date.split('-')[1]
const day = 1
return `${year}-${month}-${day}`
}
console.log(formatParamsStart('2023-6-5')) // 2023-6-1
6、格式化传入的日期得出当月的最后一天:2023-6-30
const formatParamsEnd = (date) => {
const year = date.split('-')[0]
const month = date.split('-')[1]
const day = new Date(year, month, 0).getDate()
return `${year}-${month}-${day}`
}
console.log(formatParamsEnd('2023-6-5')) // 2023-6-30
7、获取当前年月:2023-6
const monthNow = () => {
const year = new Date().getFullYear()
const month = new Date().getMonth() + 1
return `${year}-${month}`
}
8、以15号为分界线,计算当前年月
如果当前为15号或者15号之前,默认为上上个月;
如果当前15号过后,则为上个月。
const monthProfit = () => {
let year = new Date().getFullYear()
let month = new Date().getMonth() + 1
let day = new Date().getDate()
let monthNow = ''
if (day >= 15) {
// 默认月为上个月
monthNow = month - 1
} else {
// 默认月为上上个月
monthNow = month - 2
}
// 计算临界值
if (monthNow == 0) {
year--
monthNow = 12
} else if (monthNow == -1) {
year--
monthNow = 11
}
return `${year}-${monthNow}`
}
9、适用于微信小程序的倒计时
/**
*
* @param {string} page 为参数page对象this
* @param {number} timestamp 截止时间戳
* @param {string} type 倒计时时间类型
* @param {function} callbackFn 倒计时结束回调函数
*/
const countDown = (page, timestamp, type, callbackFn = () => {}) => {
let _this = page
let nowTime = new Date()
let endTime = ''
if (type === 'time') {
let currentTime = new Date().getTime()
endTime = new Date(currentTime + timestamp)
} else if (type === 'date') {
endTime = new Date(timestamp * 1000)
}
let time = endTime.getTime() - nowTime.getTime()
if (timestamp <= 0 || time <= 0) {
// 时间戳小于或者等于零返回 '00:00:00'
_this.setData({
countDownStr: '00:00:00',
isTimeOver: true
})
// 倒计时结束回调函数执行
if (typeof callbackFn === 'function') {
callbackFn.call(_this)
}
} else {
// 时间戳大于0 进行倒计时
let countTimer = setInterval(() => {
let countDownStr = ''
if (type === 'date') {
let nowTime = new Date()
let endTime = endTime = new Date(timestamp * 1000)
time = endTime.getTime() - nowTime.getTime()
}
if (time <= 0) {
_this.setData({
countDownStr: '00:00:00',
isTimeOver: true
})
if (typeof callbackFn === 'function') {
callbackFn.call(_this)
clearInterval(countTimer)
}
return
}
let day = Math.floor(time / 1000 / 60 / 60 / 24)
let hour = Math.floor(time / 1000 / 60 / 60 % 24)
let min = Math.floor(time / 1000 / 60 % 60)
let sec = Math.floor(time / 1000 % 60)
if (hour < 10) {
hour = '0' + hour
}
if (min < 10) {
min = '0' + min
}
if (sec < 10) {
sec = '0' + sec
}
// 处理格式
// if (day == '0' && hour == '00' && min == '00') {
// countDownStr = sec
// } else if (day == '0' && hour == '00') {
// countDownStr = min + ':' + sec
// } else if (day == '0') {
// countDownStr = hour + ':' + min + ':' + sec
// } else {
// countDownStr = day + ':' + hour + ':' + min + ':' + sec
// }
if (day == '0') {
countDownStr = hour + ':' + min + ':' + sec
} else {
countDownStr = day + ':' + hour + ':' + min + ':' + sec
}
_this.setData({
countDownStr: countDownStr
})
if (type === 'time') {
time = time - 1000
}
}, 1000)
}
}