date-fns#getDay TypeScript Examples
The following examples show how to use
date-fns#getDay.
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: dateUtils.tsx From symphony-ui-toolkit with Apache License 2.0 | 6 votes |
export function getDaysNeededForLastMonth(date: Date, locale: Locale) {
const firstDayOfWeek = startOfWeek(date, { locale });
const startDate = startOfMonth(date);
// getDay return the value of the weekday from 0 to 6
// i.e. startDate is a Wednesday -> getDay(startDate) = 3
// i.e. firstDayOfWeek is a Sunday -> getDay(firstDayOfWeek) = 0 (depends on locale)
// + 7 and the modulo to ensure the result is positive value is between 0 and 6 (javascript % can return negative value)
return (getDay(startDate) - getDay(firstDayOfWeek) + 7) % 7;
}
Example #2
Source File: dateUtils.tsx From symphony-ui-toolkit with Apache License 2.0 | 6 votes |
export function getDaysNeededForNextMonth(date: Date, locale: Locale) {
const daysNeededForLastMonth = getDaysNeededForLastMonth(date, locale);
const lastDayOfWeek = endOfWeek(date, { locale });
const endDate = lastDayOfMonth(date);
// getDay return the value of the weekday from 0 to 6
// i.e enDate is the Friday -> getDay(enDate) = 5
// i.e. lastDayOfWeek is a Saturday -> getDay(lastDayOfWeek) = 6 (depends on locale)
// + 7 and the modulo to ensure the result is positive value is between 0 and 6 (javascript % can return negative value)
let daysNeededForNextMonth =
(getDay(lastDayOfWeek) - getDay(endDate) + 7) % 7;
if (daysNeededForLastMonth + getDaysInMonth(date) <= 35) {
daysNeededForNextMonth += 7;
}
return daysNeededForNextMonth;
}
Example #3
Source File: MonthlyBody.tsx From react-calendar with MIT License | 6 votes |
handleOmittedDays = ({
days,
omitDays,
locale,
}: OmittedDaysProps) => {
let headings = daysInWeek({ locale });
let daysToRender = days;
//omit the headings and days of the week that were passed in
if (omitDays) {
headings = daysInWeek({ locale }).filter(
day => !omitDays.includes(day.day)
);
daysToRender = days.filter(day => !omitDays.includes(getDay(day)));
}
// omit the padding if an omitted day was before the start of the month
let firstDayOfMonth = getDay(daysToRender[0]) as number;
if (omitDays) {
let subtractOmittedDays = omitDays.filter(day => day < firstDayOfMonth)
.length;
firstDayOfMonth = firstDayOfMonth - subtractOmittedDays;
}
let padding = new Array(firstDayOfMonth).fill(0);
return { headings, daysToRender, padding };
}
Example #4
Source File: WeeklyCalendar.tsx From react-calendar with MIT License | 6 votes |
DayButton = ({ day }: DayButtonProps) => {
let { locale, week, selectedDay, changeSelectedDay } = useWeeklyCalendar();
let isSelected = selectedDay ? getDay(selectedDay) === day.day : false;
let currentDate = setDay(week, day.day, { locale });
return (
<li
onClick={() => changeSelectedDay(isSelected ? undefined : currentDate)}
className="rc-bg-white rc-cursor-pointer"
aria-label="Day of Week"
>
<div
className={`rc-rounded-lg rc-border sm:rc-w-36 rc-text-center rc-py-4 ${
isSelected
? 'rc-border-indigo-600'
: 'rc-border-gray-300 hover:rc-border-gray-500'
}`}
>
<p className="rc-font-medium rc-text-sm rc-text-gray-800">
{day.label} {format(currentDate, 'do', { locale })}
</p>
</div>
</li>
);
}
Example #5
Source File: dateUtils.tsx From symphony-ui-toolkit with Apache License 2.0 | 5 votes |
export function getFirstDayOfWeek(date: Date, locale: Locale) {
return getDay(startOfWeek(date, { locale }));
}
Example #6
Source File: lock-screen.component.ts From ng-ant-admin with MIT License | 5 votes |
getDays(date: NzSafeAny) {
return getDay(date);
}
Example #7
Source File: ngx-mat-datefns-date-adapter.ts From ngx-mat-datefns-date-adapter with MIT License | 5 votes |
getDayOfWeek(date: Date): number {
return getDay(date);
}
Example #8
Source File: DateSelect.tsx From UUI with MIT License | 4 votes |
DateSelect = UUIFunctionComponent({
name: 'DateSelect',
nodes: {
Root: 'div',
Calendar: 'div',
WeekGrid: 'div',
WeekItem: 'div',
DayGrid: 'div',
DayItem: 'div',
},
propTypes: DateSelectPropTypes,
}, (props: DateSelectFeatureProps, { nodes, NodeDataProps }) => {
const {
Root, Calendar,
WeekGrid, WeekItem,
DayGrid, DayItem,
} = nodes
const betweenIncludeDates = useCallback((date: Date, range: [Date, Date] | Date[]) => {
return !isBefore(date, range[0]) && !isAfter(date, range[1])
}, [])
const dateInfo = useMemo(() => {
const firstDayInMonth = startOfMonth(props.yearMonth)
const weekdayOfFirstDayInMonth = getDay(firstDayInMonth)
const weekdays = range(0, 7).map((i) => {
let date = new Date(props.yearMonth)
date = startOfWeek(date)
date = add(date, { days: i })
return {
key: format(date, 'yyyy-MM-dd'),
date: date,
label: format(date, 'EEEEEE', { locale: zhCN }),
};
});
const days = range(
1 - weekdayOfFirstDayInMonth,
1 - weekdayOfFirstDayInMonth + 6*7,
).map((i) => {
const date = add(firstDayInMonth, { days: i - 1 })
const selected = props.selectedDates.findIndex((i) => isSameDay(date, i)) !== -1
const inSelectedRange = (() => {
if (props.selectedDates.length >= 2) {
return betweenIncludeDates(date, props.selectedDates)
}
return false;
})()
return {
key: format(date, 'yyyy-MM-dd'),
date: date,
label: getDate(date),
active: isSameMonth(props.yearMonth, date),
selected: selected,
inSelectedRange: inSelectedRange,
}
})
return {
weekdays,
days
}
}, [props.yearMonth, props.selectedDates, betweenIncludeDates])
return (
<Root>
<Calendar>
<WeekGrid>
{dateInfo.weekdays.map((weekday) => {
return (
<WeekItem key={weekday.key}>
{weekday.label}
</WeekItem>
);
})}
</WeekGrid>
<DayGrid
onMouseLeave={() => {
props.onHoverDateChange && props.onHoverDateChange(undefined)
}}
>
{dateInfo.days.map((day) => {
const hovering = props.hoverDate ? isSameDay(day.date, props.hoverDate) : false
const inHoverRange = (() => {
if (props.selectedDates.length === 1 && props.hoverDate) {
return betweenIncludeDates(day.date, [props.selectedDates[0], props.hoverDate].sort((i, j) => Number(i) - Number(j)))
}
return false
})()
return (
<DayItem
{...NodeDataProps({
'active': day.active,
'selected': day.selected,
'in-selected-range': day.inSelectedRange,
'in-hover-range': inHoverRange,
'hovering': hovering,
})}
key={day.key}
onClick={() => {
props.onSelect(day.date)
}}
onMouseEnter={() => {
props.onHoverDateChange && props.onHoverDateChange(day.date)
}}
onMouseLeave={() => {
props.onHoverDateChange && props.onHoverDateChange(undefined)
}}
>
{day.label}
</DayItem>
)
})}
</DayGrid>
</Calendar>
</Root>
)
})