1.获取当前日期,并格式化为yyyy-MM-dd hh:mm:ss
getNowDate(){
const date= new Date();
const year= date.getFullYear();
const month= (date.getMonth() + 1).toString().padStart(2, '0');
const day= date.getDate().toString().padStart(2, '0');
const hour= date.getHours().toString().padStart(2, '0');
const minute= date.getMinutes().toString().padStart(2, '0');
const second= date.getSeconds().toString().padStart(2, '0');
const nowDate= `${year}-${month}-${day} ${hour}:${minute}:${second}`
console.log("nowDate", nowDate); //nowDate 2024-08-15 10:29:23
return nowDate
},
2.yyyy-MM-dd hh:mm:ss、yyyy-MM-dd和yyyy-MM-dd hh:mm格式的日期都转换成yyyy-MM-dd的日期格式
clearTime(date) {
if (!!date) {
if (date.length > 11) {
console.log("date1",date);
console.log("$$",date.replace(/^(\d{4})-(\d{2})-(\d{2})\s(\d{0,2}):{0,1}(\d{0,2}):{0,1}(\d{0,2})$/, "$1-$2-$3"));
return date.replace(/^(\d{4})-(\d{2})-(\d{2})\s(\d{0,2}):{0,1}(\d{0,2}):{0,1}(\d{0,2})$/, "$1-$2-$3")
} else {
console.log("date2",date);
return date
}
}
},
let date= "2024-08-10 12:12:12"
let _date= "2024-08-10"
let __date= "2024-08-10 12:12"
clearTime(date)
clearTime(_date)
clearTime(__date)