// YYYY-MM-DD 原生实现
console.log(Intl.DateTimeFormat("zh", { separator: "-", dateStyle: "short", timeStyle: "medium", }).format(),'DateTimeFormat',)
// 2023/12/16 09:14:55
console.log(new Date().toLocaleString("zh-CN", { separator: "-", /* year: "numeric", month: "2-digit", day: "2-digit" */}),'toLocaleDateString',)
// 2023/12/16 09:14:55
console.log(Intl.DateTimeFormat("zh", { separator: "-", dateStyle: "short", timeStyle: "medium", }).format()?.replace(/\s|\W/g, '.'),'DateTimeFormat',)
// 2023.12.16.09.14.55
console.log(new Date().toLocaleString("zh-CN", { separator: "-", /* year: "numeric", month: "2-digit", day: "2-digit" */})?.replace(/\s|\W/g, '.'),'toLocaleDateString',)
// 2023.12.16.09.14.55
//日期格式化函数 YYYY-MM-DD
formatDate(date) {
var d = date ? new Date(date) : new Date();
var month = '' + (d.getMonth() + 1);
var day = '' + d.getDate();
var year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
},
//七天前日期
const curDate = new Date().toLocaleDateString()
const endTime = curDate.replace(/[/]/g, '-')
const cur = new Date(); // 获取当前时间对象
const pastDate = new Date(cur.getTime() - (7 * 24 * 3600 * 1000))
const startTime = pastDate.toLocaleDateString().replace(/[/]/g, '-')
this.quickForm.docTime[0] = startTime
this.quickForm.docTime[1] = endTime
日期转json
console.log(today.toString()); //Mon Nov 11 2019 11:19:12 GMT+0800 (中国标准时间)
console.log(today.toISOString()); //2019-11-11T03:19:12.534Z
console.log(today.toTimeString()); //11:19:12 GMT+0800 (中国标准时间)
console.log(today.toUTCString()); //Mon, 11 Nov 2019 03:19:12 GMT
console.log(today.toLocaleDateString()); //2019/11/11
console.log(today.toLocaleString()); //2019/11/11 上午11:19:12
console.log(today.toLocaleString('chinese',{hour12:false})); //2019/12/11 14:17:18
console.log(today.toLocaleTimeString()); //上午11:19:12
js日期格式化7天前起止日期以及YYYY-MM-DD 原生实现
于 2021-08-13 15:05:04 首次发布