42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
export const formatTime = (date: Date) => {
|
|
const year = date.getFullYear()
|
|
const month = date.getMonth() + 1
|
|
const day = date.getDate()
|
|
const hour = date.getHours()
|
|
const minute = date.getMinutes()
|
|
const second = date.getSeconds()
|
|
|
|
return (
|
|
[year, month, day].map(formatNumber).join('/') +
|
|
' ' +
|
|
[hour, minute, second].map(formatNumber).join(':')
|
|
)
|
|
}
|
|
|
|
const formatNumber = (n: number) => {
|
|
const s = n.toString()
|
|
return s[1] ? s : '0' + s
|
|
}
|
|
|
|
export const getMonthDay = () => {
|
|
const now = new Date();
|
|
const year = now.getFullYear();
|
|
const month = now.getMonth(); // 0~11,0代表1月
|
|
|
|
// 当月第一天
|
|
const firstDay = new Date(year, month, 1);
|
|
// 当月最后一天:下个月的第0天
|
|
const lastDay = new Date(year, month + 1, 0);
|
|
|
|
// 格式化日期为 YYYY-MM-DD
|
|
const format = (date) => {
|
|
const y = date.getFullYear();
|
|
const m = (date.getMonth() + 1).toString().padStart(2, '0');
|
|
const d = date.getDate().toString().padStart(2, '0');
|
|
return `${y}-${m}-${d}`;
|
|
};
|
|
|
|
const firstStr = format(firstDay); // 例如 2026-06-01
|
|
const lastStr = format(lastDay); // 例如 2026-06-30
|
|
return [firstStr,lastStr]
|
|
} |