2 * Pending date-fn to implement UTC functions https://github.com/date-fns/date-fns/issues/376
4 export { default as eachDayOfInterval } from './eachDayOfInterval';
5 export { default as startOfDay } from './startOfDay';
6 export { default as endOfDay } from './endOfDay';
7 export { default as startOfWeek } from './startOfWeek';
8 export { default as endOfWeek } from './endOfWeek';
9 export { default as getWeekNumber } from './getWeekNumber';
10 export { default as differenceInCalendarDays } from './differenceInCalendarDays';
11 export { default as differenceInCalendarWeeks } from './differenceInCalendarWeeks';
12 export { default as differenceInCalendarYears } from './differenceInCalendarYears';
14 export const startOfYear = (date: Date) => {
15 return new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
17 export const endOfYear = (date: Date) => {
18 return new Date(Date.UTC(date.getUTCFullYear() + 1, 0, 0, 23, 59, 59, 999));
21 export const startOfMonth = (date: Date) => {
22 return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1));
24 export const endOfMonth = (date: Date) => {
25 return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 0, 23, 59, 59, 999));
28 export const min = (a: Date, b: Date) => {
29 return +a > +b ? b : a;
32 export const max = (a: Date, b: Date) => {
33 return +a > +b ? a : b;
36 export const addMilliseconds = (date: Date, amount: number) => new Date(date.getTime() + amount);
38 export const MILLISECONDS_IN_MINUTE = 60000;
39 export const addMinutes = (date: Date, amount: number) => addMilliseconds(date, amount * MILLISECONDS_IN_MINUTE);
41 export const addDays = (date: Date, amount: number) => {
42 const result = new Date(date);
43 result.setUTCDate(date.getUTCDate() + amount);
47 export const addWeeks = (date: Date, amount: number) => {
48 return addDays(date, amount * 7);
51 export const getDaysInMonth = (date: Date) => {
52 const year = date.getUTCFullYear();
53 const monthIndex = date.getUTCMonth();
54 const lastDayOfMonth = new Date(0);
55 lastDayOfMonth.setUTCFullYear(year, monthIndex + 1, 0);
56 lastDayOfMonth.setUTCHours(0, 0, 0, 0);
57 return lastDayOfMonth.getUTCDate();
60 export const addMonths = (date: Date, amount: number) => {
61 const result = new Date(+date);
62 const desiredMonth = date.getUTCMonth() + amount;
63 const dateWithDesiredMonth = new Date(0);
64 dateWithDesiredMonth.setUTCFullYear(date.getUTCFullYear(), desiredMonth, 1);
65 dateWithDesiredMonth.setUTCHours(0, 0, 0, 0);
66 const daysInMonth = getDaysInMonth(dateWithDesiredMonth);
67 // Set the last day of the new month
68 // if the original date was the last day of the longer month
69 result.setUTCMonth(desiredMonth, Math.min(daysInMonth, date.getUTCDate()));
73 export const addYears = (date: Date, amount: number) => {
74 return addMonths(date, amount * 12);
77 export const isSameYear = (dateLeft: Date, dateRight: Date) => {
78 return dateLeft.getUTCFullYear() === dateRight.getUTCFullYear();
81 export const isSameMonth = (dateLeft: Date, dateRight: Date) => {
82 if (!isSameYear(dateLeft, dateRight)) {
85 return dateLeft.getUTCMonth() === dateRight.getUTCMonth();
88 export const isSameDay = (dateLeft: Date, dateRight: Date) => {
89 if (!isSameMonth(dateLeft, dateRight)) {
92 return dateLeft.getUTCDate() === dateRight.getUTCDate();
95 export const isSameHour = (dateLeft: Date, dateRight: Date) => {
96 if (!isSameDay(dateLeft, dateRight)) {
99 return dateLeft.getUTCHours() === dateRight.getUTCHours();
103 * Check if a later date happens on the following day to an earlier date
104 * @param {Date} dateLeft Earlier date
105 * @param {Date} dateRight Later date
107 export const isNextDay = (dateLeft: Date, dateRight: Date) => {
108 const tomorrow = new Date(Date.UTC(dateLeft.getUTCFullYear(), dateLeft.getUTCMonth(), dateLeft.getUTCDate() + 1));
109 return isSameDay(tomorrow, dateRight);
112 export { default as format } from './format';