feat(ui): standardize collapsible filters and date selection UI
This commit is contained in:
@@ -135,3 +135,51 @@ export const formatDateTime = (datetime: string): string => {
|
||||
hour12: false,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 獲取日期區間(YYYY-MM-DD 格式)
|
||||
* 支援: today, yesterday, this_week, this_month, last_month
|
||||
*/
|
||||
export const getDateRange = (type: string): { start: string, end: string } => {
|
||||
const now = new Date();
|
||||
// Reset time to avoid timezone issues when calculating dates
|
||||
now.setHours(12, 0, 0, 0);
|
||||
|
||||
let start = new Date(now);
|
||||
let end = new Date(now);
|
||||
|
||||
const format = (d: Date) => {
|
||||
const year = d.getFullYear();
|
||||
const month = String(d.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getDate()).padStart(2, "0");
|
||||
return `${year}-${month}-${day}`;
|
||||
};
|
||||
|
||||
switch (type) {
|
||||
case "today":
|
||||
break;
|
||||
case "yesterday":
|
||||
start.setDate(now.getDate() - 1);
|
||||
end.setDate(now.getDate() - 1);
|
||||
break;
|
||||
case "this_week":
|
||||
// 週一為一週的第一天
|
||||
const dayOfWeek = now.getDay() || 7;
|
||||
start.setDate(now.getDate() - dayOfWeek + 1);
|
||||
end.setDate(now.getDate() + (7 - dayOfWeek));
|
||||
break;
|
||||
case "this_month":
|
||||
start = new Date(now.getFullYear(), now.getMonth(), 1);
|
||||
end = new Date(now.getFullYear(), now.getMonth() + 1, 0);
|
||||
break;
|
||||
case "last_month":
|
||||
start = new Date(now.getFullYear(), now.getMonth() - 1, 1);
|
||||
end = new Date(now.getFullYear(), now.getMonth(), 0);
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
start: format(start),
|
||||
end: format(end)
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user