|
// 返回年月日
|
export const timestampToTime = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp)
|
var Y = date.getFullYear() + "-"
|
var M =
|
(date.getMonth() + 1 < 10
|
? "0" + (date.getMonth() + 1)
|
: date.getMonth() + 1) + "-"
|
var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate())
|
return Y + M + D
|
}
|
|
//返回年月日 时分
|
export const timestampToMinute = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp)
|
var Y = date.getFullYear() + "-"
|
var M =
|
(date.getMonth() + 1 < 10
|
? "0" + (date.getMonth() + 1)
|
: date.getMonth() + 1) + "-"
|
var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
|
var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
|
var m = (date.getMinutes() < 10
|
? "0" + (date.getMinutes())
|
: date.getMinutes())
|
var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
|
return Y + M + D + h + m
|
}
|
|
//返回年月日 时分秒
|
export const timestampToHour = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp)
|
var Y = date.getFullYear() + "-"
|
var M =
|
(date.getMonth() + 1 < 10
|
? "0" + (date.getMonth() + 1)
|
: date.getMonth() + 1) + "-"
|
var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
|
var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
|
var m = (date.getMinutes() < 10
|
? "0" + (date.getMinutes())
|
: date.getMinutes()) + ':'
|
var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
|
return Y + M + D + h + m + s
|
}
|
//返回年月日时分秒
|
export const timeStampTosecond = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp)
|
var Y = date.getFullYear() + "-"
|
var M =
|
(date.getMonth() + 1 < 10
|
? "0" + (date.getMonth() + 1)
|
: date.getMonth() + 1) + "-"
|
var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
|
var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
|
var m = (date.getMinutes() < 10
|
? "0" + (date.getMinutes())
|
: date.getMinutes()) + ':'
|
var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
|
return Y + M + D + h + "00:00"
|
}
|
|
export const getTimeBefore = (n = 8) => {
|
const nowStamp = new Date().getTime()
|
const beforeStamp = nowStamp - (n * 60 * 60 * 1000)
|
const nowTime = timeStampTosecond(nowStamp + 60 * 1000 * 60)
|
const beforeTime = timeStampTosecond(beforeStamp)
|
return [beforeTime, nowTime]
|
}
|
export const Timesecend = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp)
|
var Y = date.getFullYear() + "-"
|
var M =
|
(date.getMonth() + 1 < 10
|
? "0" + (date.getMonth() + 1)
|
: date.getMonth() + 1) + "-"
|
var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
|
var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
|
var m = (date.getMinutes() < 10
|
? "0" + (date.getMinutes())
|
: date.getMinutes()) + ':'
|
var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
|
return M + D + h + m + s
|
}
|
|
export const TimeMinute = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp)
|
var Y = date.getFullYear() + "-"
|
var M =
|
(date.getMonth() + 1 < 10
|
? "0" + (date.getMonth() + 1)
|
: date.getMonth() + 1) + "-"
|
var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
|
var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
|
var m = (date.getMinutes() < 10
|
? "0" + (date.getMinutes())
|
: date.getMinutes())
|
var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
|
return M + D + h + m
|
}
|
// export const
|
|
function timeData (time) {
|
var date
|
if (time) {
|
date = new Date(time)
|
} else {
|
date = new Date()
|
}
|
const Y = date.getFullYear()
|
const M = date.getMonth() + 1
|
const MM = (date.getMonth() + 1 < 10) ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
|
const D = date.getDate()
|
const DD = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate())
|
const h = date.getHours()
|
const hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours())
|
const m = date.getMinutes()
|
const mm = (date.getMinutes() < 10
|
? "0" + (date.getMinutes())
|
: date.getMinutes())
|
const s = date.getSeconds()
|
const ss = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
|
return {
|
Y, M, MM, D, DD, h, hh, m, mm, s, ss
|
}
|
}
|
|
export const getEightHourTime = (time) => {
|
const { Y, M, MM, DD, D, hh, h, mm, m, ss, s } = timeData(time)
|
const now = `${Y}-${MM}-${DD} ${hh}:00:00`
|
if (h > 8) {
|
const before = `${Y}-${MM}-${DD} 08:00:00`
|
return [before, now]
|
} else {
|
if (D - 1 > 0) {
|
const yesD = (DD - 1 < 10) ? ('0' + DD - 1) : DD - 1
|
const yesterday = `${Y}-${MM}-${yesD} 09:00:00`
|
return [yesterday, now]
|
} else {
|
if (MM - 1 > 0) {
|
const yesMM = (MM - 1 < 10) ? ('0' + MM - 1) : MM - 1
|
const yesDay = new Date(Y, yesMM, 0).getDate()
|
const lastMonDay = `${Y}-${yesMM}-${yesDay} 09:00:00`
|
return [lastMonDay, now]
|
} else {
|
const lastY = Y - 1
|
const lastYDay = `${lastY}-12-31 09:00:00`
|
return [lastYDay, now]
|
}
|
}
|
}
|
}
|
|
export const get24BeforeTime = (time) => {
|
const { Y, M, MM, DD, D, hh, h, mm, m, ss, s } = timeData(time)
|
const nowHour = `${Y}-${MM}-${DD} ${hh - 0 + 1}:00:00`
|
const date = new Date(nowHour)
|
const { Y: yY, MM: yM, DD: yD, hh: yH, mm: ymm, ss: yss } = timeData(date.setDate(date.getDate() - 1))
|
const yesterDay = `${yY}-${yM}-${yD} ${yH}:00:00`
|
return [yesterDay, nowHour]
|
}
|