// YYYY-MM-DD 原生实现
console.log(Intl.DateTimeFormat("zh", { separator: "-", dateStyle: "short", timeStyle: "medium", }).format(),'DateTimeFormat',)
日期转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
export const getData_YYYMMDD = (howLongDayAgo: number, type?: 'month',justYYYYDD?:boolean): string => {
const currentDate = new Date();
if(howLongDayAgo){
if (type==='month') {
currentDate.setMonth(currentDate.getMonth() - howLongDayAgo);
} else {
currentDate.setDate(currentDate.getDate() - howLongDayAgo);
}
}
const options: {
year: string;
month: string;
day?: string;
} = { year: 'numeric', month: '2-digit', day: '2-digit' };
if (justYYYYDD) {
delete options.day
}
return currentDate.toLocaleDateString('en-CA', options); // 使用加拿大英语格式
}