1. 2022-07-21 00:00:00————2022-07-21
dateFormat(data) {
if (data) {
let date = new Date(data.replace(/-/g, '/'));
let year = date.getFullYear();
let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
//let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
//let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
//let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return year + "-" + month + "-" + day;
}
},
2. 一个时间与当前时间做比较
let now = new Date() //当前时间
//如果传2022-02-22 14:15:00要转化成 2022/02/22 14:15:00
let startTime = this.hiddList.RecPeriod
console.log(this.hiddList.RecPeriod)
let end = new Date(startTime.replace(/-/g,"/")) //对比时间
console.log(now.getTime())
console.log(end.getTime())
if(now.getTime() > end.getTime()){
//当前时间比指定时间大
this.int = 1
}else{
//当前时间比指定时间小
this.int = 2
}
这篇博客主要介绍了JavaScript中如何将日期字符串转换为标准格式,并进行时间比较。示例代码展示了将'2022-07-21'格式的日期转化为'2022/07/21',并比较此日期与当前时间的方法,用于判断当前时间是否在指定日期之后。

被折叠的 条评论
为什么被折叠?



