2024-03-12 05:34:36 +00:00
|
|
|
|
export function formatDate(time: any, cFormat: string) {
|
|
|
|
|
const format = cFormat || "{y}-{m}-{d}";
|
|
|
|
|
const date = new Date(time);
|
|
|
|
|
const formatObj:any = {
|
|
|
|
|
//年
|
|
|
|
|
y: date.getFullYear(),
|
|
|
|
|
//月
|
|
|
|
|
m: date.getMonth() + 1,
|
|
|
|
|
//日
|
|
|
|
|
d: date.getDate(),
|
|
|
|
|
//小时
|
|
|
|
|
h: date.getHours(),
|
|
|
|
|
//分钟
|
|
|
|
|
i: date.getMinutes(),
|
|
|
|
|
//秒
|
|
|
|
|
s: date.getSeconds(),
|
|
|
|
|
//星期
|
|
|
|
|
a: date.getDay(),
|
|
|
|
|
};
|
|
|
|
|
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
|
|
|
|
const value = formatObj[key];
|
|
|
|
|
// Note: getDay() returns 0 on Sunday
|
|
|
|
|
if (key === "a") {
|
|
|
|
|
//如果key是a,就是星期,格式化成一~日
|
|
|
|
|
//例如formatDate(new Date(), '{y}-{m}-{d}-{h}-{i}-{s}-{a}');
|
|
|
|
|
//会输出2021-10-29-00-00-00-五
|
|
|
|
|
//星期的value会返回0-6,['日', '一', '二', '三', '四', '五', '六'][2]代表周二
|
|
|
|
|
return ["日", "一", "二", "三", "四", "五", "六"][value];
|
|
|
|
|
}
|
|
|
|
|
//padStart用于字符串头部补全,2个字符,如果不够前面补0
|
|
|
|
|
return value.toString().padStart(2, "0");
|
|
|
|
|
});
|
|
|
|
|
return time_str;
|
|
|
|
|
}
|
2024-03-15 02:40:59 +00:00
|
|
|
|
|
|
|
|
|
export function getCurrentMonthStartAndEnd() {
|
|
|
|
|
let now = new Date();
|
|
|
|
|
let currentMonthStart = new Date(now.getFullYear(), now.getMonth(), 1);
|
|
|
|
|
let currentMonthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
start: currentMonthStart,
|
|
|
|
|
end: currentMonthEnd
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getCurrentYearStartAndEnd() {
|
|
|
|
|
let now = new Date();
|
|
|
|
|
let currentYearStart = new Date(now.getFullYear(), 0, 1); // 0 代表一月
|
|
|
|
|
let currentYearEnd = (new Date(now.getFullYear() + 1, 0, 1) as any) - 1; // 下一年的一月一日减一毫秒
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
start: currentYearStart,
|
|
|
|
|
end: currentYearEnd
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|