总体代码如下,可直接拷贝
// 获取当前的本机时间
//先new一个时间对象
let now = new Date();
//在通过getTime获取当前时间, 必须加一天
let nowTime= new Date(now.getTime() + 24 * 60 * 60 * 1000);
//年
let years = nowTime.getFullYear()
//月份
let month = nowTime.getMonth() < 10
? '0' + (nowTime.getMonth() + 1)
: nowTime.getMonth() + 1
//日
let day = nowTime.getDate() < 10
? '0' + nowTime.getDate()
: nowTime.getDate()
//时
let hours = nowTime.getHours() < 10
? '0' + nowTime.getHours()
: nowTime.getHours()
//分
let minute = nowTime.getMinutes() < 10
? '0' + nowTime.getMinutes()
: nowTime.getMinutes()
//秒
let second = nowTime.getSeconds() < 10
? '0' + nowTime.getSeconds()
: nowTime.getSeconds()
let nowDay = years + "-" + month + '-' + day + ' ' + hours + ':' + minute
// 2023-11-27 19:15
为什么要判断<10 给前面加个0呢. 因为美观一些,如果不加的话,一串时间里看到单位数有点突兀