Fix RFM date range consistency
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m5s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m5s
This commit is contained in:
58
src/dateRanges.ts
Normal file
58
src/dateRanges.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import type { DateRange } from './types';
|
||||
|
||||
export const startOfLocalDay = (date: Date): Date => {
|
||||
const nextDate = new Date(date);
|
||||
nextDate.setHours(0, 0, 0, 0);
|
||||
return nextDate;
|
||||
};
|
||||
|
||||
export const endOfLocalDay = (date: Date): Date => {
|
||||
const nextDate = new Date(date);
|
||||
nextDate.setHours(23, 59, 59, 999);
|
||||
return nextDate;
|
||||
};
|
||||
|
||||
export const rangeForDay = (date: Date): DateRange => ({
|
||||
start: startOfLocalDay(date),
|
||||
end: endOfLocalDay(date),
|
||||
});
|
||||
|
||||
export const rangeForLastDays = (days: number, today = new Date()): DateRange => {
|
||||
const end = endOfLocalDay(today);
|
||||
const start = startOfLocalDay(today);
|
||||
start.setDate(start.getDate() - Math.max(days - 1, 0));
|
||||
return { start, end };
|
||||
};
|
||||
|
||||
export const rangeForPreviousDay = (today = new Date()): DateRange => {
|
||||
const date = startOfLocalDay(today);
|
||||
date.setDate(date.getDate() - 1);
|
||||
return rangeForDay(date);
|
||||
};
|
||||
|
||||
export const parseLocalDateInput = (value: string): Date | null => {
|
||||
if (!value) return null;
|
||||
|
||||
const match = value.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
||||
if (!match) return null;
|
||||
|
||||
const [, yearValue, monthValue, dayValue] = match;
|
||||
const date = new Date(Number(yearValue), Number(monthValue) - 1, Number(dayValue));
|
||||
|
||||
if (
|
||||
date.getFullYear() !== Number(yearValue) ||
|
||||
date.getMonth() !== Number(monthValue) - 1 ||
|
||||
date.getDate() !== Number(dayValue)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return date;
|
||||
};
|
||||
|
||||
export const formatDateParam = (date: Date): string => {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
};
|
||||
Reference in New Issue
Block a user