var nowDate = ''
var weekDay = ''
var nowTime = ''
function getTime() {
const date = new Date();
const Y = date.getFullYear();
const M = date.getMonth() + 1;
const D = date.getDate();
const MM = M < 10 ? '0' + M : M;
const DD = D < 10 ? '0' + D : D;
const arr = ['日', '一', '二', '三', '四', '五', '六'];
nowDate = Y + '-' + MM + '-' + DD;
weekDay = '星期' + arr[date.getDay()];
const h = date.getHours();
const m = date.getMinutes();
const s = date.getSeconds();
const hh = h < 10 ? '0' + h : h;
const mm = m < 10 ? '0' + m : m;
const ss = s < 10 ? '0' + s : s;
nowTime = hh + ':' + mm + ':' + ss;
}
getTime()
console.log('nowDate', nowDate) // 2023-10-1
console.log('weekDay', weekDay) // 星期日
console.log('nowTime', nowTime) // 11:15:01
获取今天-明天-后天
addDayCount 参数:
0代表当天的日期 以0为中心点
1就是明天以此类推
-1就是昨天以此类推
breaking分割符:比如 - / ** 等
export function getDateStr(addDayCount, breaking) {
const day = new Date();
day.setDate(day.getDate() + addDayCount);
const y = day.getFullYear();
const m = day.getMonth() + 1;
const d = day.getDate();
const mm = m < 10 ? '0' + m : m;
const dd = d < 10 ? '0' + d : d;
return y + breaking + mm + breaking + dd;
}