UNPKG

7.24 kBJavaScriptView Raw
1/*
2 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { Months } from "./months";
17export function isDateValid(date) {
18 return date instanceof Date && !isNaN(date.valueOf());
19}
20export function areEqual(date1, date2) {
21 if (date1 == null && date2 == null) {
22 return true;
23 }
24 else if (date1 == null || date2 == null) {
25 return false;
26 }
27 else {
28 return date1.getTime() === date2.getTime();
29 }
30}
31export function areRangesEqual(dateRange1, dateRange2) {
32 if (dateRange1 == null && dateRange2 == null) {
33 return true;
34 }
35 else if (dateRange1 == null || dateRange2 == null) {
36 return false;
37 }
38 else {
39 const [start1, end1] = dateRange1;
40 const [start2, end2] = dateRange2;
41 const areStartsEqual = (start1 == null && start2 == null) || areSameDay(start1, start2);
42 const areEndsEqual = (end1 == null && end2 == null) || areSameDay(end1, end2);
43 return areStartsEqual && areEndsEqual;
44 }
45}
46export function areSameDay(date1, date2) {
47 return areSameMonth(date1, date2) && date1.getDate() === date2.getDate();
48}
49export function areSameMonth(date1, date2) {
50 return (date1 != null &&
51 date2 != null &&
52 date1.getMonth() === date2.getMonth() &&
53 date1.getFullYear() === date2.getFullYear());
54}
55export function areSameTime(date1, date2) {
56 return (date1 != null &&
57 date2 != null &&
58 date1.getHours() === date2.getHours() &&
59 date1.getMinutes() === date2.getMinutes() &&
60 date1.getSeconds() === date2.getSeconds() &&
61 date1.getMilliseconds() === date2.getMilliseconds());
62}
63export function clone(d) {
64 return new Date(d.getTime());
65}
66export function isDayInRange(date, dateRange, exclusive = false) {
67 if (date == null) {
68 return false;
69 }
70 const day = clone(date);
71 const start = clone(dateRange[0]);
72 const end = clone(dateRange[1]);
73 day.setHours(0, 0, 0, 0);
74 start.setHours(0, 0, 0, 0);
75 end.setHours(0, 0, 0, 0);
76 return start <= day && day <= end && (!exclusive || (!areSameDay(start, day) && !areSameDay(day, end)));
77}
78export function isDayRangeInRange(innerRange, outerRange) {
79 return ((innerRange[0] == null || isDayInRange(innerRange[0], outerRange)) &&
80 (innerRange[1] == null || isDayInRange(innerRange[1], outerRange)));
81}
82export function isMonthInRange(date, dateRange) {
83 if (date == null) {
84 return false;
85 }
86 const day = clone(date);
87 const start = clone(dateRange[0]);
88 const end = clone(dateRange[1]);
89 day.setDate(1);
90 start.setDate(1);
91 end.setDate(1);
92 day.setHours(0, 0, 0, 0);
93 start.setHours(0, 0, 0, 0);
94 end.setHours(0, 0, 0, 0);
95 return start <= day && day <= end;
96}
97export const isTimeEqualOrGreaterThan = (time, timeToCompare) => time.getTime() >= timeToCompare.getTime();
98export const isTimeEqualOrSmallerThan = (time, timeToCompare) => time.getTime() <= timeToCompare.getTime();
99export function isTimeInRange(date, minDate, maxDate) {
100 const time = getDateOnlyWithTime(date);
101 const minTime = getDateOnlyWithTime(minDate);
102 const maxTime = getDateOnlyWithTime(maxDate);
103 const isTimeGreaterThanMinTime = isTimeEqualOrGreaterThan(time, minTime);
104 const isTimeSmallerThanMaxTime = isTimeEqualOrSmallerThan(time, maxTime);
105 if (isTimeEqualOrSmallerThan(maxTime, minTime)) {
106 return isTimeGreaterThanMinTime || isTimeSmallerThanMaxTime;
107 }
108 return isTimeGreaterThanMinTime && isTimeSmallerThanMaxTime;
109}
110export function getTimeInRange(time, minTime, maxTime) {
111 if (areSameTime(minTime, maxTime)) {
112 return maxTime;
113 }
114 else if (isTimeInRange(time, minTime, maxTime)) {
115 return time;
116 }
117 else if (isTimeSameOrAfter(time, maxTime)) {
118 return maxTime;
119 }
120 return minTime;
121}
122/**
123 * Returns true if the time part of `date` is later than or equal to the time
124 * part of `dateToCompare`. The day, month, and year parts will not be compared.
125 */
126export function isTimeSameOrAfter(date, dateToCompare) {
127 const time = getDateOnlyWithTime(date);
128 const timeToCompare = getDateOnlyWithTime(dateToCompare);
129 return isTimeEqualOrGreaterThan(time, timeToCompare);
130}
131/**
132 * @returns a Date at the exact time-wise midpoint between startDate and endDate
133 */
134export function getDateBetween(dateRange) {
135 const start = dateRange[0].getTime();
136 const end = dateRange[1].getTime();
137 const middle = start + (end - start) * 0.5;
138 return new Date(middle);
139}
140export function getDateTime(date, time) {
141 if (date == null) {
142 return null;
143 }
144 else if (time == null) {
145 // cannot use default argument because `null` is a common value in this package.
146 return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0);
147 }
148 else {
149 return new Date(date.getFullYear(), date.getMonth(), date.getDate(), time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
150 }
151}
152export function getDateOnlyWithTime(date) {
153 return new Date(0, 0, 0, date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
154}
155export function getDatePreviousMonth(date) {
156 if (date.getMonth() === Months.JANUARY) {
157 return new Date(date.getFullYear() - 1, Months.DECEMBER);
158 }
159 else {
160 return new Date(date.getFullYear(), date.getMonth() - 1);
161 }
162}
163export function getDateNextMonth(date) {
164 if (date.getMonth() === Months.DECEMBER) {
165 return new Date(date.getFullYear() + 1, Months.JANUARY);
166 }
167 else {
168 return new Date(date.getFullYear(), date.getMonth() + 1);
169 }
170}
171export function convert24HourMeridiem(hour, toPm) {
172 if (hour < 0 || hour > 23) {
173 throw new Error(`hour must be between [0,23] inclusive: got ${hour}`);
174 }
175 return toPm ? (hour % 12) + 12 : hour % 12;
176}
177export function getIsPmFrom24Hour(hour) {
178 if (hour < 0 || hour > 23) {
179 throw new Error(`hour must be between [0,23] inclusive: got ${hour}`);
180 }
181 return hour >= 12;
182}
183export function get12HourFrom24Hour(hour) {
184 if (hour < 0 || hour > 23) {
185 throw new Error(`hour must be between [0,23] inclusive: got ${hour}`);
186 }
187 const newHour = hour % 12;
188 return newHour === 0 ? 12 : newHour;
189}
190export function get24HourFrom12Hour(hour, isPm) {
191 if (hour < 1 || hour > 12) {
192 throw new Error(`hour must be between [1,12] inclusive: got ${hour}`);
193 }
194 const newHour = hour === 12 ? 0 : hour;
195 return isPm ? newHour + 12 : newHour;
196}
197export function isToday(date) {
198 return areSameDay(date, new Date());
199}
200//# sourceMappingURL=dateUtils.js.map
\No newline at end of file