web/src/utils/dateUtils.ts

129 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export const formatDate = (date: Date|string): any => {
if (date === undefined || date === null) {
return '-';
}
let dateDetail: Date;
if (date instanceof Date) {
dateDetail = date;
} else {
dateDetail = new Date(date as string);
if (isNaN(dateDetail.getTime())) {
return '-';
}
}
const year = dateDetail.getFullYear();
const month = String(dateDetail.getMonth() + 1).padStart(2, '0');
const day = String(dateDetail.getDate()).padStart(2, '0');
const hours = String(dateDetail.getHours()).padStart(2, '0');
const minutes = String(dateDetail.getMinutes()).padStart(2, '0');
const seconds = String(dateDetail.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
export const formatListTime = (date: any) => {
let dateDetail: Date;
if (date instanceof Date) {
dateDetail = date;
} else if (typeof date === 'string') {
dateDetail = new Date(date);
if (isNaN(dateDetail.getTime())) {
return '-';
}
} else {
return '-';
}
const year = dateDetail.getFullYear();
const month = String(dateDetail.getMonth() + 1).padStart(2, '0');
const day = String(dateDetail.getDate()).padStart(2, '0');
const hours = dateDetail.getHours();
const minutes = String(dateDetail.getMinutes()).padStart(2, '0');
const seconds = String(dateDetail.getSeconds()).padStart(2, '0');// 将24小时制转换为12小时制
//如果不是今年 返回年
if (dateDetail.getFullYear() !== new Date().getFullYear()) {
return `${year}`;
}
//如果是今天,返回时和分,如果不是今天,返回月和日
if (dateDetail.toDateString() === new Date().toDateString()) {
return `${hours}:${minutes}`;
} else {
return `${month}-${day}`;
}
}
export const formatDateArray = (dates: Date[]): string[] => {
return dates.map(date => formatDate(date));
};
export const getToday = () => {
const today = new Date();
return { start: formatDate(new Date(today.setHours(0, 0, 0, 0))), end: formatDate(new Date(today.setHours(23, 59, 59, 999))) };
};
export const getYesterday = () => {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return { start: formatDate(new Date(yesterday.setHours(0, 0, 0, 0))), end: formatDate(new Date(yesterday.setHours(23, 59, 59, 999))) };
};
export const getTomorrow = () => {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
return { start: formatDate(new Date(tomorrow.setHours(0, 0, 0, 0))), end: formatDate(new Date(tomorrow.setHours(23, 59, 59, 999))) };
};
export const getThisWeek = () => {
const today = new Date();
const startOfWeek = new Date(today.setDate(today.getDate() - today.getDay()));
const endOfWeek = new Date(today.setDate(today.getDate() - today.getDay() + 6));
return { start: formatDate(new Date(startOfWeek.setHours(0, 0, 0, 0))), end: formatDate(new Date(endOfWeek.setHours(23, 59, 59, 999))) };
};
export const getThisMonth = () => {
const today = new Date();
const startOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
const endOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);
return { start: formatDate(new Date(startOfMonth.setHours(0, 0, 0, 0))), end: formatDate(new Date(endOfMonth.setHours(23, 59, 59, 999))) };
};
export const getDaysBetweenDates = (startDateStr: string, endDateStr: string): string[] => {
const startDate = new Date(startDateStr);
const endDate = new Date(endDateStr);
const days: string[] = [];
let currentDate = new Date(startDate);
while (currentDate <= endDate) {
days.push(formatDate(currentDate).split(' ')[0]);
currentDate.setDate(currentDate.getDate() + 1);
}
return days;
};
export const getEndOfDay = (date: Date | string): string => {
let dateDetail: Date;
if (date instanceof Date) {
dateDetail = new Date(date);
} else {
dateDetail = new Date(date);
if (isNaN(dateDetail.getTime())) {
return '-';
}
}
dateDetail.setHours(23, 59, 59, 0); // 设置为当天 23:59:00
return formatDate(dateDetail).slice(0, 19); //
};
export const getCurrentDate=()=> {
const date = new Date();
const year = date.getFullYear();
// 月份从0开始所以要加1
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始需要+1并补0
const day = String(date.getDate()).padStart(2, '0'); // 补0操作
return `${year}-${month}-${day}`;
}