vue 时间格式相互转化
1、转化YYYYMMDDHHMMSS时间格式为YYYY-MM-DD HH:MM:ss
export function changeTime(time) {
var newtime = ''
newtime = time.substring(0, 4) + '-' + time.substring(4, 6) + '-' + time.substring(6, 8) + ' ' + time.substring(8, 10) + ':' + time.substring(10, 12) + ':' + time.substring(12, 14)
return newtime
}
2、转化时间戳格式为 YYYY-MM-DD HH:MM:ss
export function timestampToTime(timestamp) {
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
}
3、将类似格式2020-02-11T12:24:18.000+0000 转化为 YYYY-MM-DD HH:MM:ss
export function renderTime(date) {
var dateee = new Date(date).toJSON();
return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}
4、通过时间戳判断是否过期 若过期返回1 未过期返回0
export function TimeIsPassed(time) {
var currentTime = Date.parse(new Date())
if(time >= currentTime){
return 0
}
else{
return 1
}
}
5、通过时间戳获取距当前时间之间相隔的天数
export function TimestampBetweenDays(data){
var now = new Date().getTime()
return Math.floor((data - now) / (24*60*60*1000))
}
6、毫秒转换为天数
export function MicrosecondChangeDays(data) {
return Math.floor(data / 86400000)
}