Skip to content

Instantly share code, notes, and snippets.

@kitze
Last active June 21, 2020 19:56
Show Gist options
  • Save kitze/b0e72300cee1fb7194e2bd8ccf4091f3 to your computer and use it in GitHub Desktop.
Save kitze/b0e72300cee1fb7194e2bd8ccf4091f3 to your computer and use it in GitHub Desktop.
good ol' date-fns
import {
formatDistance,
isAfter,
isBefore,
endOfDay,
startOfDay,
isSameDay,
isSameSecond,
isSameMinute,
isSameHour,
isSameWeek,
isSameISOWeek,
isSameMonth,
isSameQuarter,
isSameYear,
addDays,
subDays
} from 'date-fns';
export const distanceInWordsToNow = date => formatDistance(date, new Date());
export const isFuture = date => isAfter(date, new Date());
export const isPast = date => isBefore(date, new Date());
export const endOfToday = () => endOfDay(new Date());
export const endOfTomorrow = () => endOfDay(addDays(new Date(), 1));
export const endOfYesterday = () => endOfDay(subDays(new Date(), 1));
export const startOfToday = () => startOfDay(new Date());
export const startOfTomorrow = () => startOfDay(addDays(new Date(), 1));
export const startOfYesterday = () => startOfDay(subDays(new Date(), 1));
export const isToday = date => isSameDay(new Date(), date);
export const isTomorrow = date => isSameDay(date, addDays(new Date(), 1));
export const isYesterday = date => isSameDay(date, subDays(new Date(), 1));
export const isThisSecond = date => isSameSecond(date, new Date());
export const isThisMinute = date => isSameMinute(date, new Date());
export const isThisHour = date => isSameHour(date, new Date());
export const isThisWeek = date => isSameWeek(date, new Date());
export const isThisISOWeek = date => isSameISOWeek(date, new Date());
export const isThisMonth = date => isSameMonth(date, new Date());
export const isThisQuarter = date => isSameQuarter(date, new Date());
export const isThisYear = date => isSameYear(date, new Date());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment