export function formatDate(inputDateStr: string): string {
const inputDate = new Date(inputDateStr);
if (isNaN(inputDate.getTime())) {
throw new Error("Invalid date string");
}
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const inputDay = new Date(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate());
const timeDiff = today.getTime() - inputDay.getTime();
const dayDiff = timeDiff / (1000 * 60 * 60 * 24);
const hours = inputDate.getHours().toString().padStart(2, '0');
const minutes = inputDate.getMinutes().toString().padStart(2, '0');
const timePart = `${hours}:${minutes}`;
if (dayDiff < 0) {
throw new Error("Input date is in the future");
} else if (dayDiff === 0) {
return `今天 ${timePart}`;
} else if (dayDiff === 1) {
return `昨天 ${timePart}`;
} else if (dayDiff <= 7) {
const weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
const dayOfWeek = weekDays[inputDay.getDay()];
return `${dayOfWeek} ${timePart}`;
} else {
const month = (inputDate.getMonth() + 1).toString().padStart(2, '0');
const day = inputDate.getDate().toString().padStart(2, '0');
return `更久以前:${month}-${day} ${timePart}`;
}
}