let nowDate = Date.now();
let dateObj = new Date(nowDate);
let formattedDate = dateObj.toLocaleString(); // 使用默认的日期和时间格式
console.log(formattedDate); // 2023/10/10 14:47:24
会获取年、月、日、小时、分钟和秒,并用适当的格式进行填充,然后拼接成最终的日期时间字符串:
let nowDate = Date.now();
let dateObj = new Date(nowDate);
let year = dateObj.getFullYear();
let month = String(dateObj.getMonth() + 1).padStart(2, '0');
let day = String(dateObj.getDate()).padStart(2, '0');
let hours = String(dateObj.getHours()).padStart(2, '0');
let minutes = String(dateObj.getMinutes()).padStart(2, '0');
let seconds = String(dateObj.getSeconds()).padStart(2, '0');
let formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // yyyy-mm-dd hh:mm:ss
console.log(formattedDate); // 2023-10-10 14:47:24