date-fns#startOfYesterday TypeScript Examples
The following examples show how to use
date-fns#startOfYesterday.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts From gio-design with Apache License 2.0 | 7 votes |
parseStartAndEndDate = (timeRange: string | undefined): [Date | undefined, Date | undefined] => {
if (!timeRange || timeRange.split(':').length !== 2) {
return [undefined, undefined];
}
const items = timeRange.split(':');
const times = items[1].split(',').map((str) => parseInt(str, 10));
const today = startOfToday();
const yesterday = startOfYesterday();
if (items[0] === 'since') {
if (times.length === 1) {
return [new Date(times[0]), today];
}
return [new Date(times[0]), sub(today, { days: times[1] })];
}
if (items[0] === 'since-lt-today') {
if (times.length === 1) {
return [new Date(times[0]), yesterday];
}
return [new Date(times[0]), sub(yesterday, { days: times[1] })];
}
if (items[0] === 'abs') {
return [new Date(times[0]), new Date(times[1])];
}
if (items[0] === 'day') {
return [sub(today, { days: times[0] - 1 }), sub(today, { days: times[1] })];
}
return [undefined, undefined];
}
Example #2
Source File: RelativeRangeBody.tsx From gio-design with Apache License 2.0 | 6 votes |
function RelativeRangeBody({ dateRange, fixedMode, onRangeChange, disabledDate }: RelativeRangeBodyProps) {
if (fixedMode) {
const handleOnSelect = (current: Date) => {
onRangeChange([startOfDay(current), startOfYesterday()]);
};
return <DatePicker disabledDate={disabledDate} value={dateRange[0]} onSelect={handleOnSelect} />;
}
return <DateRangePicker disabledDate={disabledDate} onSelect={onRangeChange} value={dateRange as [Date, Date]} />;
}
Example #3
Source File: RelativeRangePicker.tsx From gio-design with Apache License 2.0 | 6 votes |
function RelativeRangePicker({ disabledDate, timeRange, onSelect, onCancel, ...rest }: RangePickerProps) {
const defaultDates = parseStartAndEndDate(timeRange ?? 'day:2,1');
const [dates, setDates] = React.useState<[Date, Date]>(defaultDates as [Date, Date]);
const [endDateHidden, setEndDateHidden] = React.useState<boolean>(isYesterday(dates[1]));
const inputDisabled = !isNil(disabledDate);
const handleDisabledDate = (current: Date) =>
disabledDate?.(current) || isAfter(startOfDay(current), endDateHidden ? startOfYesterday() : startOfToday());
const handleOnOK = () => {
onSelect(`day:${differenceInDays(startOfToday(), dates[0]) + 1},${differenceInDays(startOfToday(), dates[1])}`);
};
return (
<InnerRangePanel
data-testid="relative-range-picker"
disableOK={!isValid(dates[0]) || !isValid(dates[1])}
header={<RelativeRangeHeader inputDisabled={inputDisabled} dateRange={dates} onRangeChange={setDates} onModeChange={setEndDateHidden} />}
body={
<RelativeRangeBody
dateRange={dates}
fixedMode={endDateHidden}
disabledDate={handleDisabledDate}
onRangeChange={setDates}
/>
}
onCancel={onCancel}
onOK={handleOnOK}
{...rest}
/>
);
}
Example #4
Source File: SinceRangePicker.tsx From gio-design with Apache License 2.0 | 5 votes |
function SinceRangePicker({ disabledDate, timeRange, onSelect, onCancel, experimental, ...rest }: RangePickerProps) {
const endDateKeys = ['today', experimental ? 'yesterday' : undefined];
const dates = parseStartAndEndDate(timeRange);
const prefixCls = usePrefixCls('range-panel__header');
const [startDate, setStartDate] = React.useState<Date | undefined>(dates[0]);
const [endKey, setEndKey] = React.useState(
endDateKeys[timeRange && timeRange.split(':')[0] === 'since' ? 0 : 1] || 0
);
const locale = useLocale<typeof defaultLocale>('StaticPastTimePicker') || defaultLocale;
const { startDayText, FromText, toTodayText, toYesterdayText } = {
...defaultLocale,
...locale,
};
const END_DATE_MAPPING: Record<string, string> = {
today: toTodayText,
yesterday: toYesterdayText,
};
const renderHeader = () => {
const startDateString = startDate ? format(startDate, DATE_FORMAT) : startDayText;
return (
<>
<span className={`${prefixCls}__text`}>{`${FromText} ${startDateString}`}</span>
<span className={`${prefixCls}__options`}>
<Switch
size="small"
defaultValue={endKey}
onChange={(e) => {
setEndKey(e.target.value);
}}
>
{endDateKeys.map(
(o: string) =>
o && (
<Switch.Item key={o} value={o}>
{END_DATE_MAPPING[o]}
</Switch.Item>
)
)}
</Switch>
</span>
</>
);
};
const handleDisabledDate = (current: Date) =>
disabledDate?.(current) || isAfter(current, endKey === 'yesterday' ? startOfYesterday() : startOfToday());
const handleOnOK = () => {
onSelect(`${endKey === 'yesterday' ? 'since-lt-today' : 'since'}:${getTime(startOfDay(startDate as Date))}`);
};
return (
<InnerRangePanel
disableOK={!isValid(startDate)}
header={renderHeader()}
body={<DatePicker disabledDate={handleDisabledDate} value={startDate} onSelect={setStartDate} />}
onCancel={onCancel}
onOK={handleOnOK}
{...rest}
/>
);
}