|
1 | 1 | const { sub, add, format, getUnixTime } = require('date-fns');
|
2 | 2 |
|
3 |
| -const shortDayFormat = 'd'; |
4 |
| -const longDayFormat = 'dd'; |
5 |
| -const shortMonthFormat = 'M'; |
6 |
| -const longMonthFormat = 'MM'; |
7 |
| -const longYearFormat = 'yyyy'; |
8 |
| - |
9 |
| -const today = new Date(); |
10 |
| -const todayDay = format(today, longDayFormat); |
11 |
| -const todayMonth = format(today, longMonthFormat); |
12 |
| -const todayYear = format(today, longYearFormat); |
13 |
| -const todayFullString = format(today, 'dd MMM yyyy'); |
14 |
| - |
15 |
| -// to test cannot be issued in past |
16 |
| -const fourDaysAgo = sub(today, { days: 4 }); |
17 |
| -const fourDaysAgoDay = format(fourDaysAgo, longDayFormat); |
18 |
| -const fourDaysAgoMonth = format(fourDaysAgo, longMonthFormat); |
19 |
| -const fourDaysAgoYear = format(fourDaysAgo, longYearFormat); |
20 |
| -const fourDaysAgoFull = format(fourDaysAgo, 'd MMMM yyyy'); |
21 |
| - |
22 |
| -const yesterday = sub(today, { days: 1 }); |
23 |
| -const yesterdayUnix = getUnixTime(yesterday).toString(); |
24 |
| -const yesterdayDay = format(yesterday, longDayFormat); |
25 |
| -const yesterdayMonth = format(yesterday, longMonthFormat); |
26 |
| -const yesterdayYear = format(yesterday, longYearFormat); |
27 |
| - |
28 |
| -const twoDaysAgo = sub(today, { days: 2 }); |
29 |
| - |
30 |
| -const oneMonth = add(today, { months: 1 }); |
31 |
| -const oneMonthUnix = getUnixTime(oneMonth).toString(); |
32 |
| -const oneMonthFormattedFull = format(oneMonth, 'dd MMMM yyyy'); |
33 |
| -const oneMonthFormattedTable = format(oneMonth, 'd MMMM yyyy'); |
34 |
| -const oneMonthFormattedShort = format(oneMonth, 'dd MMM yyyy'); |
35 |
| -const oneMonthDay = format(oneMonth, longDayFormat); |
36 |
| -const oneMonthMonth = format(oneMonth, longMonthFormat); |
37 |
| -const oneMonthYear = format(oneMonth, longYearFormat); |
38 |
| - |
39 |
| -const twoMonths = add(today, { months: 2 }); |
40 |
| -const twoMonthsDay = format(twoMonths, longDayFormat); |
41 |
| -const twoMonthsMonth = format(twoMonths, longMonthFormat); |
42 |
| -const twoMonthsYear = format(twoMonths, longYearFormat); |
43 |
| -const twoMonthsFormatted = format(twoMonths, 'dd MMM yyyy'); |
44 |
| -const twoMonthsFormattedFull = format(twoMonths, 'dd MMMM yyyy'); |
45 |
| -const twoMonthsFormattedTable = format(twoMonths, 'd MMMM yyyy'); |
46 |
| - |
47 |
| -// to test that if beyond issue/ cover start date limit |
48 |
| -const tomorrow = add(today, { days: 1 }); |
49 |
| -const tomorrowDay = format(tomorrow, longDayFormat); |
50 |
| -const tomorrowMonth = format(tomorrow, longMonthFormat); |
51 |
| -const tomorrowYear = format(tomorrow, longYearFormat); |
52 |
| - |
53 |
| -const twoDays = add(today, { days: 2 }); |
54 |
| -const twoDaysDay = format(twoDays, longDayFormat); |
55 |
| -const twoDaysMonth = format(twoDays, longMonthFormat); |
56 |
| -const twoDaysYear = format(twoDays, longYearFormat); |
57 |
| - |
58 |
| -// 25 days ago |
59 |
| -const twentyFiveDaysAgo = sub(today, { days: 25 }); |
60 |
| -const twentyFiveDaysAgoUnix = getUnixTime(twentyFiveDaysAgo).toString(); |
61 |
| -const twentyFiveDaysAgoFormatted = format(twentyFiveDaysAgo, 'dd MMM yyyy'); |
62 |
| - |
63 |
| -// 35 days ago |
64 |
| -const thirtyFiveDaysAgo = sub(today, { days: 35 }); |
65 |
| -const thirtyFiveDaysAgoUnix = getUnixTime(thirtyFiveDaysAgo).toString(); |
66 |
| -const thirtyFiveDaysAgoFormatted = format(thirtyFiveDaysAgo, 'dd MMM yyyy'); |
67 |
| - |
68 |
| -const threeMonths = add(today, { months: 3 }); |
69 |
| -const threeMonthsDay = format(threeMonths, longDayFormat); |
70 |
| -const threeMonthsMonth = format(threeMonths, longMonthFormat); |
71 |
| -const threeMonthsYear = format(threeMonths, longYearFormat); |
72 |
| - |
73 |
| -const threeMonthsOneDay = add(today, { months: 3, days: 1 }); |
74 |
| -const threeMonthsOneDayDay = format(threeMonthsOneDay, longDayFormat); |
75 |
| -const threeMonthsOneDayMonth = format(threeMonthsOneDay, longMonthFormat); |
76 |
| -const threeMonthsOneDayYear = format(threeMonthsOneDay, longYearFormat); |
77 |
| -const threeMonthsOneDayFullString = format(threeMonthsOneDay, 'dd MMM yyyy'); |
78 |
| -const threeMonthsOneDayFullMonthString = format(threeMonthsOneDay, 'd MMMM yyyy'); |
79 |
| - |
80 |
| -const twentyEight = add(today, { days: 28 }); |
81 |
| -const twentyEightDay = format(twentyEight, longDayFormat); |
82 |
| -const twentyEightMonth = format(twentyEight, longMonthFormat); |
83 |
| -const twentyEightYear = format(twentyEight, longYearFormat); |
84 |
| - |
85 |
| -const threeDaysAgo = sub(today, { days: 3 }); |
86 |
| -const threeDaysAgoUnix = getUnixTime(threeDaysAgo).toString(); |
87 |
| -const threeDaysDay = format(threeDaysAgo, longDayFormat); |
88 |
| -const threeDaysMonth = format(threeDaysAgo, longMonthFormat); |
89 |
| -const threeDaysYear = format(threeDaysAgo, longYearFormat); |
90 |
| - |
91 |
| -const sevenDaysAgo = sub(today, { days: 7 }); |
92 |
| -const sevenDaysAgoUnix = getUnixTime(sevenDaysAgo).toString(); |
93 |
| -const sevenDaysAgoDay = format(sevenDaysAgo, longDayFormat); |
94 |
| -const sevenDaysAgoMonth = format(sevenDaysAgo, longMonthFormat); |
95 |
| -const sevenDaysAgoYear = format(sevenDaysAgo, longYearFormat); |
96 |
| - |
97 |
| -const sevenDays = add(today, { days: 7 }); |
98 |
| -const sevenDaysUnix = getUnixTime(sevenDays).toString(); |
99 |
| -const sevenDaysDay = format(sevenDays, longDayFormat); |
100 |
| -const sevenDaysMonth = format(sevenDays, longMonthFormat); |
101 |
| -const sevenDaysYear = format(sevenDays, longYearFormat); |
102 |
| - |
103 |
| -const twoYears = add(today, { years: 2, months: 3, days: 1 }); |
104 |
| -const twoYearsDay = format(twoYears, longDayFormat); |
105 |
| -const twoYearsMonth = format(twoYears, longMonthFormat); |
106 |
| -const twoYearsYear = format(twoYears, longYearFormat); |
107 |
| - |
108 |
| -const twoYearsAgo = sub(today, { years: 2 }); |
109 |
| -const twoYearsAgoUnix = getUnixTime(twoYearsAgo).toString(); |
110 |
| -const twoYearsAgoDay = format(twoYearsAgo, longDayFormat); |
111 |
| -const twoYearsAgoMonth = format(twoYearsAgo, longMonthFormat); |
112 |
| -const twoYearsAgoYear = format(twoYearsAgo, longYearFormat); |
113 |
| - |
114 |
| -const threeYearsAgo = sub(today, { years: 3 }); |
115 |
| -const threeYearsAgoDay = format(threeYearsAgo, longDayFormat); |
116 |
| -const threeYearsAgoMonth = format(threeYearsAgo, longMonthFormat); |
117 |
| -const threeYearsAgoYear = format(threeYearsAgo, longYearFormat); |
118 |
| - |
119 |
| -const oneYearAgo = sub(today, { years: 1 }); |
120 |
| -const oneYearUnix = getUnixTime(oneYearAgo).toString(); |
121 |
| -const oneYearAgoDay = format(oneYearAgo, longDayFormat); |
122 |
| -const oneYearAgoMonth = format(oneYearAgo, longMonthFormat); |
123 |
| -const oneYearAgoYear = format(oneYearAgo, longYearFormat); |
124 |
| - |
125 |
| -const threeYears = add(today, { years: 3, months: 3, days: 1 }); |
126 |
| -const threeYearsDay = format(threeYears, longDayFormat); |
127 |
| -const threeYearsMonth = format(threeYears, longMonthFormat); |
128 |
| -const threeYearsYear = format(threeYears, longYearFormat); |
129 |
| - |
130 |
| -const threeDaysAgoPlusMonth = add(threeDaysAgo, { months: 3 }); |
131 |
| - |
132 |
| -const sixYearsOneDay = add(today, { years: 6, months: 0, days: 1 }); |
133 |
| -const sixYearsOneDayDay = format(sixYearsOneDay, longDayFormat); |
134 |
| -const sixYearsOneDayMonth = format(sixYearsOneDay, longMonthFormat); |
135 |
| -const sixYearsOneDayYear = format(sixYearsOneDay, longYearFormat); |
136 |
| - |
137 |
| -const twelveMonthsOneDay = add(today, { months: 12, days: 1 }); |
138 |
| -const twelveMonthsOneDayDay = format(twelveMonthsOneDay, longDayFormat); |
139 |
| -const twelveMonthsOneDayMonth = format(twelveMonthsOneDay, longMonthFormat); |
140 |
| -const twelveMonthsOneDayYear = format(twelveMonthsOneDay, longYearFormat); |
141 |
| - |
142 |
| -const twelveMonthsOneDayAgo = sub(today, { months: 12, days: 1 }); |
143 |
| -const twelveMonthsOneDayAgoDay = format(twelveMonthsOneDayAgo, longDayFormat); |
144 |
| -const twelveMonthsOneDayAgoMonth = format(twelveMonthsOneDayAgo, longMonthFormat); |
145 |
| -const twelveMonthsOneDayAgoYear = format(twelveMonthsOneDayAgo, longYearFormat); |
146 |
| - |
147 |
| -const todayUnix = getUnixTime(today).toString(); |
148 |
| -const todayUnixDay = format(threeDaysAgo, longDayFormat); |
149 |
| -const todayUnixMonth = format(threeDaysAgo, longMonthFormat); |
150 |
| -const todayUnixYear = format(threeDaysAgo, longYearFormat); |
151 |
| - |
152 |
| -const todayFormattedFull = format(today, 'dd MMMM yyyy'); |
153 |
| -const todayFormatted = format(today, 'd MMMM yyyy'); |
154 |
| -const todayFormattedShort = format(today, 'dd MMM yyyy'); |
155 |
| -const tomorrowFormattedFull = format(tomorrow, 'd MMMM yyyy'); |
156 |
| -const tomorrowFormattedFacilityPage = format(tomorrow, 'dd MMM yyyy'); |
157 |
| -const tomorrowUnix = getUnixTime(tomorrow).toString(); |
158 |
| - |
159 |
| -const todayFormattedTimeHours = format(today, 'h'); |
160 |
| -const todayFormattedTimeAmPm = format(today, 'aaa'); |
| 3 | +export const SHORT_DAY_FORMAT = 'd'; |
| 4 | +export const LONG_DAY_FORMAT = 'dd'; |
| 5 | +export const SHORT_MONTH_FORMAT = 'M'; |
| 6 | +export const LONG_MONTH_FORMAT = 'MM'; |
| 7 | +export const LONG_YEAR_FORMAT = 'yyyy'; |
| 8 | +export const DD_MMM_YYYY_FORMAT = 'dd MMM yyyy'; |
| 9 | +export const D_MMMM_YYYY_FORMAT = 'd MMMM yyyy'; |
| 10 | +export const DD_MMMM_YYYY_FORMAT = 'dd MMMM yyyy'; |
| 11 | +export const TIME_HOURS_FORMAT = 'h'; |
| 12 | +export const TIME_AM_PM_FORMAT = 'aaa'; |
| 13 | + |
| 14 | +const getFormattedValues = (date) => ({ |
| 15 | + date, |
| 16 | + day: format(date, SHORT_DAY_FORMAT), |
| 17 | + dayLong: format(date, LONG_DAY_FORMAT), |
| 18 | + month: format(date, SHORT_MONTH_FORMAT), |
| 19 | + monthLong: format(date, LONG_MONTH_FORMAT), |
| 20 | + year: format(date, LONG_YEAR_FORMAT), |
| 21 | + dd_MMM_yyyy: format(date, DD_MMM_YYYY_FORMAT), |
| 22 | + d_MMMM_yyyy: format(date, D_MMMM_YYYY_FORMAT), |
| 23 | + dd_MMMM_yyyy: format(date, DD_MMMM_YYYY_FORMAT), |
| 24 | + unixSecondsString: getUnixTime(date).toString(), |
| 25 | + unixMillisecondsString: date.valueOf().toString(), |
| 26 | + unixMilliseconds: date.valueOf(), |
| 27 | +}); |
| 28 | + |
| 29 | +const todayDate = new Date(); |
| 30 | + |
| 31 | +// Today |
| 32 | +export const today = getFormattedValues(todayDate); |
| 33 | + |
| 34 | +// Dates in the future |
| 35 | +export const tomorrow = getFormattedValues(add(todayDate, { days: 1 })); |
| 36 | +export const twoDays = getFormattedValues(add(todayDate, { days: 2 })); |
| 37 | +export const threeDays = getFormattedValues(add(todayDate, { days: 3 })); |
| 38 | +export const sevenDays = getFormattedValues(add(todayDate, { days: 7 })); |
| 39 | +export const twentyEightDays = getFormattedValues(add(todayDate, { days: 28 })); |
| 40 | +export const oneMonth = getFormattedValues(add(todayDate, { months: 1 })); |
| 41 | +export const twoMonths = getFormattedValues(add(todayDate, { months: 2 })); |
| 42 | +export const threeMonthsMinusThreeDays = getFormattedValues(add(todayDate, { months: 3, days: -3 })); |
| 43 | +export const threeMonths = getFormattedValues(add(todayDate, { months: 3 })); |
| 44 | +export const threeMonthsOneDay = getFormattedValues(add(todayDate, { months: 3, days: 1 })); |
| 45 | +export const oneYear = getFormattedValues(add(todayDate, { years: 1 })); |
| 46 | +export const twelveMonthsOneDay = getFormattedValues(add(todayDate, { months: 12, days: 1 })); |
| 47 | +export const twoYears = getFormattedValues(add(todayDate, { years: 2 })); |
| 48 | +export const threeYears = getFormattedValues(add(todayDate, { years: 3 })); |
| 49 | +export const sixYearsOneDay = getFormattedValues(add(todayDate, { years: 6, months: 0, days: 1 })); |
| 50 | + |
| 51 | +// Dates in the past |
| 52 | +export const yesterday = getFormattedValues(sub(todayDate, { days: 1 })); |
| 53 | +export const twoDaysAgo = getFormattedValues(sub(todayDate, { days: 2 })); |
| 54 | +export const threeDaysAgo = getFormattedValues(sub(todayDate, { days: 3 })); |
| 55 | +export const fourDaysAgo = getFormattedValues(sub(todayDate, { days: 4 })); |
| 56 | +export const sevenDaysAgo = getFormattedValues(sub(todayDate, { days: 7 })); |
| 57 | +export const twentyFiveDaysAgo = getFormattedValues(sub(todayDate, { days: 25 })); |
| 58 | +export const thirtyFiveDaysAgo = getFormattedValues(sub(todayDate, { days: 35 })); |
| 59 | +export const twelveMonthsOneDayAgo = getFormattedValues(sub(todayDate, { months: 12, days: 1 })); |
| 60 | +export const oneYearAgo = getFormattedValues(sub(todayDate, { years: 1 })); |
| 61 | +export const twoYearsAgo = getFormattedValues(sub(todayDate, { years: 2 })); |
| 62 | + |
| 63 | +// Times |
| 64 | +export const todayTimeHours = format(todayDate, TIME_HOURS_FORMAT); |
| 65 | +export const todayTimeAmPm = format(todayDate, TIME_AM_PM_FORMAT); |
161 | 66 |
|
162 | 67 | /**
|
163 | 68 | * Some tests check that validation errors appear,
|
164 | 69 | * If a year is entered with zero as a letter. I.e, O, instead of 0.
|
165 | 70 | */
|
166 |
| -const yearWithZeroLetter = '2O22'; |
167 |
| - |
168 |
| -module.exports = { |
169 |
| - shortDayFormat, |
170 |
| - longDayFormat, |
171 |
| - shortMonthFormat, |
172 |
| - longMonthFormat, |
173 |
| - longYearFormat, |
174 |
| - today, |
175 |
| - todayDay, |
176 |
| - todayMonth, |
177 |
| - todayYear, |
178 |
| - tomorrow, |
179 |
| - tomorrowDay, |
180 |
| - tomorrowMonth, |
181 |
| - tomorrowYear, |
182 |
| - tomorrowUnix, |
183 |
| - twoDays, |
184 |
| - twoDaysDay, |
185 |
| - twoDaysMonth, |
186 |
| - twoDaysYear, |
187 |
| - twentyFiveDaysAgo, |
188 |
| - twentyFiveDaysAgoUnix, |
189 |
| - twentyFiveDaysAgoFormatted, |
190 |
| - thirtyFiveDaysAgo, |
191 |
| - thirtyFiveDaysAgoUnix, |
192 |
| - thirtyFiveDaysAgoFormatted, |
193 |
| - yesterday, |
194 |
| - yesterdayDay, |
195 |
| - yesterdayMonth, |
196 |
| - yesterdayYear, |
197 |
| - yesterdayUnix, |
198 |
| - twoDaysAgo, |
199 |
| - fourDaysAgo, |
200 |
| - fourDaysAgoDay, |
201 |
| - fourDaysAgoMonth, |
202 |
| - fourDaysAgoYear, |
203 |
| - fourDaysAgoFull, |
204 |
| - oneMonth, |
205 |
| - oneMonthUnix, |
206 |
| - oneMonthFormattedTable, |
207 |
| - oneMonthFormattedFull, |
208 |
| - oneMonthFormattedShort, |
209 |
| - oneMonthDay, |
210 |
| - oneMonthMonth, |
211 |
| - oneMonthYear, |
212 |
| - twoMonths, |
213 |
| - twoMonthsDay, |
214 |
| - twoMonthsMonth, |
215 |
| - twoMonthsYear, |
216 |
| - twoMonthsFormatted, |
217 |
| - twoMonthsFormattedFull, |
218 |
| - twoMonthsFormattedTable, |
219 |
| - threeMonths, |
220 |
| - threeMonthsDay, |
221 |
| - threeMonthsMonth, |
222 |
| - threeMonthsYear, |
223 |
| - threeMonthsOneDay, |
224 |
| - threeMonthsOneDayDay, |
225 |
| - threeMonthsOneDayMonth, |
226 |
| - threeMonthsOneDayYear, |
227 |
| - sixYearsOneDay, |
228 |
| - threeMonthsOneDayFullString, |
229 |
| - threeMonthsOneDayFullMonthString, |
230 |
| - sixYearsOneDayDay, |
231 |
| - sixYearsOneDayMonth, |
232 |
| - sixYearsOneDayYear, |
233 |
| - twentyEight, |
234 |
| - twentyEightDay, |
235 |
| - twentyEightMonth, |
236 |
| - twentyEightYear, |
237 |
| - threeDaysAgo, |
238 |
| - threeDaysAgoUnix, |
239 |
| - threeDaysDay, |
240 |
| - threeDaysMonth, |
241 |
| - threeDaysYear, |
242 |
| - sevenDaysAgo, |
243 |
| - sevenDaysAgoUnix, |
244 |
| - sevenDaysAgoDay, |
245 |
| - sevenDaysAgoMonth, |
246 |
| - sevenDaysAgoYear, |
247 |
| - sevenDays, |
248 |
| - sevenDaysUnix, |
249 |
| - sevenDaysDay, |
250 |
| - sevenDaysMonth, |
251 |
| - sevenDaysYear, |
252 |
| - todayUnix, |
253 |
| - todayUnixDay, |
254 |
| - todayUnixMonth, |
255 |
| - todayUnixYear, |
256 |
| - threeDaysAgoPlusMonth, |
257 |
| - todayFullString, |
258 |
| - todayFormattedFull, |
259 |
| - todayFormatted, |
260 |
| - todayFormattedShort, |
261 |
| - tomorrowFormattedFull, |
262 |
| - tomorrowFormattedFacilityPage, |
263 |
| - todayFormattedTimeHours, |
264 |
| - todayFormattedTimeAmPm, |
265 |
| - twoYears, |
266 |
| - twoYearsDay, |
267 |
| - twoYearsMonth, |
268 |
| - twoYearsYear, |
269 |
| - twoYearsAgo, |
270 |
| - twoYearsAgoUnix, |
271 |
| - twoYearsAgoDay, |
272 |
| - twoYearsAgoMonth, |
273 |
| - twoYearsAgoYear, |
274 |
| - threeYearsAgoDay, |
275 |
| - threeYearsAgoMonth, |
276 |
| - threeYearsAgoYear, |
277 |
| - threeYears, |
278 |
| - threeYearsDay, |
279 |
| - threeYearsMonth, |
280 |
| - threeYearsYear, |
281 |
| - oneYearAgo, |
282 |
| - oneYearUnix, |
283 |
| - oneYearAgoDay, |
284 |
| - oneYearAgoMonth, |
285 |
| - oneYearAgoYear, |
286 |
| - twelveMonthsOneDayAgo, |
287 |
| - twelveMonthsOneDay, |
288 |
| - twelveMonthsOneDayDay, |
289 |
| - twelveMonthsOneDayMonth, |
290 |
| - twelveMonthsOneDayYear, |
291 |
| - twelveMonthsOneDayAgoDay, |
292 |
| - twelveMonthsOneDayAgoMonth, |
293 |
| - twelveMonthsOneDayAgoYear, |
294 |
| - yearWithZeroLetter, |
295 |
| -}; |
| 71 | +export const yearWithZeroLetter = '2O22'; |
0 commit comments