UNPKG

4.31 kBJavaScriptView Raw
1/**
2 * @module constants
3 * @summary Useful constants
4 * @description
5 * Collection of useful date constants.
6 *
7 * The constants could be imported from `date-fns/constants`:
8 *
9 * ```ts
10 * import { maxTime, minTime } from "./constants/date-fns/constants";
11 *
12 * function isAllowedTime(time) {
13 * return time <= maxTime && time >= minTime;
14 * }
15 * ```
16 */
17
18/**
19 * @constant
20 * @name daysInWeek
21 * @summary Days in 1 week.
22 */
23export const daysInWeek = 7;
24
25/**
26 * @constant
27 * @name daysInYear
28 * @summary Days in 1 year.
29 *
30 * @description
31 * How many days in a year.
32 *
33 * One years equals 365.2425 days according to the formula:
34 *
35 * > Leap year occurs every 4 years, except for years that are divisible by 100 and not divisible by 400.
36 * > 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
37 */
38export const daysInYear = 365.2425;
39
40/**
41 * @constant
42 * @name maxTime
43 * @summary Maximum allowed time.
44 *
45 * @example
46 * import { maxTime } from "./constants/date-fns/constants";
47 *
48 * const isValid = 8640000000000001 <= maxTime;
49 * //=> false
50 *
51 * new Date(8640000000000001);
52 * //=> Invalid Date
53 */
54export const maxTime = Math.pow(10, 8) * 24 * 60 * 60 * 1000;
55
56/**
57 * @constant
58 * @name minTime
59 * @summary Minimum allowed time.
60 *
61 * @example
62 * import { minTime } from "./constants/date-fns/constants";
63 *
64 * const isValid = -8640000000000001 >= minTime;
65 * //=> false
66 *
67 * new Date(-8640000000000001)
68 * //=> Invalid Date
69 */
70export const minTime = -maxTime;
71
72/**
73 * @constant
74 * @name millisecondsInWeek
75 * @summary Milliseconds in 1 week.
76 */
77export const millisecondsInWeek = 604800000;
78
79/**
80 * @constant
81 * @name millisecondsInDay
82 * @summary Milliseconds in 1 day.
83 */
84export const millisecondsInDay = 86400000;
85
86/**
87 * @constant
88 * @name millisecondsInMinute
89 * @summary Milliseconds in 1 minute
90 */
91export const millisecondsInMinute = 60000;
92
93/**
94 * @constant
95 * @name millisecondsInHour
96 * @summary Milliseconds in 1 hour
97 */
98export const millisecondsInHour = 3600000;
99
100/**
101 * @constant
102 * @name millisecondsInSecond
103 * @summary Milliseconds in 1 second
104 */
105export const millisecondsInSecond = 1000;
106
107/**
108 * @constant
109 * @name minutesInYear
110 * @summary Minutes in 1 year.
111 */
112export const minutesInYear = 525600;
113
114/**
115 * @constant
116 * @name minutesInMonth
117 * @summary Minutes in 1 month.
118 */
119export const minutesInMonth = 43200;
120
121/**
122 * @constant
123 * @name minutesInDay
124 * @summary Minutes in 1 day.
125 */
126export const minutesInDay = 1440;
127
128/**
129 * @constant
130 * @name minutesInHour
131 * @summary Minutes in 1 hour.
132 */
133export const minutesInHour = 60;
134
135/**
136 * @constant
137 * @name monthsInQuarter
138 * @summary Months in 1 quarter.
139 */
140export const monthsInQuarter = 3;
141
142/**
143 * @constant
144 * @name monthsInYear
145 * @summary Months in 1 year.
146 */
147export const monthsInYear = 12;
148
149/**
150 * @constant
151 * @name quartersInYear
152 * @summary Quarters in 1 year
153 */
154export const quartersInYear = 4;
155
156/**
157 * @constant
158 * @name secondsInHour
159 * @summary Seconds in 1 hour.
160 */
161export const secondsInHour = 3600;
162
163/**
164 * @constant
165 * @name secondsInMinute
166 * @summary Seconds in 1 minute.
167 */
168export const secondsInMinute = 60;
169
170/**
171 * @constant
172 * @name secondsInDay
173 * @summary Seconds in 1 day.
174 */
175export const secondsInDay = secondsInHour * 24;
176
177/**
178 * @constant
179 * @name secondsInWeek
180 * @summary Seconds in 1 week.
181 */
182export const secondsInWeek = secondsInDay * 7;
183
184/**
185 * @constant
186 * @name secondsInYear
187 * @summary Seconds in 1 year.
188 */
189export const secondsInYear = secondsInDay * daysInYear;
190
191/**
192 * @constant
193 * @name secondsInMonth
194 * @summary Seconds in 1 month
195 */
196export const secondsInMonth = secondsInYear / 12;
197
198/**
199 * @constant
200 * @name secondsInQuarter
201 * @summary Seconds in 1 quarter.
202 */
203export const secondsInQuarter = secondsInMonth * 3;
204
205/**
206 * @constant
207 * @name constructFromSymbol
208 * @summary Symbol enabling Date extensions to inherit properties from the reference date.
209 *
210 * The symbol is used to enable the `constructFrom` function to construct a date
211 * using a reference date and a value. It allows to transfer extra properties
212 * from the reference date to the new date. It's useful for extensions like
213 * [`TZDate`](https://github.com/date-fns/tz) that accept a time zone as
214 * a constructor argument.
215 */
216export const constructFromSymbol = Symbol.for("constructDateFrom");