JS 计算两个时间戳相差年月日时分秒
// JS 计算两个时间戳相差年月日时分秒
calculateDiffTime(startTime, endTime, type) {
let runTime = parseInt(endTime - startTime)/1000
let year = Math.floor(runTime / 86400 / 365 )
runTime = runTime % (86400 * 365)
let month = Math.floor(runTime / 86400 / 30)
runTime = runTime % (86400 * 30)
let day = Math.floor(runTime / 86400)
runTime = runTime % 86400
let hour = Math.floor(runTime / 3600)
runTime = runTime % 3600
let minute = Math.floor(runTime / 60)
runTime = runTime % 60
let second = runTime
if (type === 1) { // 返回相差年数
return year + '年'
} else if (type === 2) { // 返回相差年数月数
return year + '年' + month + '月'
} else if (type === 3) { // 返回相差年数月数天数
return year + '年' + month + '月' + day + '日'
} else { // 返回相差年数月数天数时分秒
return year + '年' + month + '月' + day + '日' + hour + '时' + minute + '分' + second + '秒'
}
},
替换字符串中所有的点号,并返回新字符串。
js替换2022.12.19中的.为2022-12-19
const regex = /\./g;
const newString = '2022.12.19'.replace(regex, '-');
// newString 的值为 '2022-12-19'
console.log(newString)
或者 分割并拼凑
formateTime(e){
var year = e.substring(0,4);
var month = e.substring(5,7);
var day = e.substring(8,10);
return year+'-'+month+'-'+day
},
替换字符串中的年月日,并返回新字符串。
js替换2022年12月19日为2022-12-19
returnAge(e){
let time = e.replace(/(\d{4}).(\d{1,2}).(\d{1,2}).+/mg, '$1-$2-$3');
let a= (new Date(time)).getTime(); //生日
let b= (new Date()).getTime(); // 今天
let c=this.calculateDiffTime(a,b,1) //求相差年数
// console.log(c)
return c
},
// JS 计算两个时间戳相差年月日时分秒
calculateDiffTime(startTime, endTime, type) {
let runTime = parseInt(endTime - startTime)
// console.log(runTime)
let year = Math.floor(runTime/86400/365/1000)
if (type == 1) { // 返回相差年数
// return year + '年'
return year
}
},