1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { Months } from "./months";
|
17 | export function isDateValid(date) {
|
18 | return date instanceof Date && !isNaN(date.valueOf());
|
19 | }
|
20 | export 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 | }
|
31 | export 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 | }
|
46 | export function areSameDay(date1, date2) {
|
47 | return areSameMonth(date1, date2) && date1.getDate() === date2.getDate();
|
48 | }
|
49 | export function areSameMonth(date1, date2) {
|
50 | return (date1 != null &&
|
51 | date2 != null &&
|
52 | date1.getMonth() === date2.getMonth() &&
|
53 | date1.getFullYear() === date2.getFullYear());
|
54 | }
|
55 | export 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 | }
|
63 | export function clone(d) {
|
64 | return new Date(d.getTime());
|
65 | }
|
66 | export 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 | }
|
78 | export function isDayRangeInRange(innerRange, outerRange) {
|
79 | return ((innerRange[0] == null || isDayInRange(innerRange[0], outerRange)) &&
|
80 | (innerRange[1] == null || isDayInRange(innerRange[1], outerRange)));
|
81 | }
|
82 | export 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 | }
|
97 | export const isTimeEqualOrGreaterThan = (time, timeToCompare) => time.getTime() >= timeToCompare.getTime();
|
98 | export const isTimeEqualOrSmallerThan = (time, timeToCompare) => time.getTime() <= timeToCompare.getTime();
|
99 | export 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 | }
|
110 | export 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 |
|
124 |
|
125 |
|
126 | export function isTimeSameOrAfter(date, dateToCompare) {
|
127 | const time = getDateOnlyWithTime(date);
|
128 | const timeToCompare = getDateOnlyWithTime(dateToCompare);
|
129 | return isTimeEqualOrGreaterThan(time, timeToCompare);
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 | export 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 | }
|
140 | export function getDateTime(date, time) {
|
141 | if (date == null) {
|
142 | return null;
|
143 | }
|
144 | else if (time == null) {
|
145 |
|
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 | }
|
152 | export function getDateOnlyWithTime(date) {
|
153 | return new Date(0, 0, 0, date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
|
154 | }
|
155 | export 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 | }
|
163 | export 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 | }
|
171 | export 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 | }
|
177 | export 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 | }
|
183 | export 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 | }
|
190 | export 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 | }
|
197 | export function isToday(date) {
|
198 | return areSameDay(date, new Date());
|
199 | }
|
200 |
|
\ | No newline at end of file |