const now =newDate();const year = now.getFullYear();const month = now.getMonth()+1;// 月份从0开始计数,所以需要加1const day = now.getDate();const months = now.getMonth();let time = year +'-'+(month <10?'0'+ month : month)+'-'+(day <10?'0'+ day : day)//输出 2024-09-10
//获取时间戳const timestamp = Date.now();// 获取当前时间戳
console.log(timestamp);// 输出:1678229456789``````javascript
//快捷方式toLocaleDateString():根据当前区域设置格式化日期部分。
toLocaleTimeString():根据当前区域设置格式化时间部分。
const now =newDate();const time = now.toLocaleDateString().replaceAll('/','-')// 2024-09-10const time = now.toLocaleTimeString()// 12:00:00 PM
时间戳转换时间
var dates=12345612var now =newDate(dates*1000);var year = now.getFullYear();var month = now.getMonth()+1;if(month <10){
month ='0'+ month
}var date = now.getDate();if(date <10){
date ='0'+ date
}
console.log( year +"-"+ month +"-"+ date)// 2024-09-10