1 | # `@shopify/dates`
|
2 |
|
3 | [![Build Status](https://travis-ci.org/Shopify/quilt.svg?branch=master)](https://travis-ci.org/Shopify/quilt)
|
4 | [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Fdates.svg)](https://badge.fury.io/js/%40shopify%2Fdates.svg) [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@shopify/dates.svg)](https://img.shields.io/bundlephobia/minzip/@shopify/dates.svg)
|
5 |
|
6 | Lightweight date operations library.
|
7 |
|
8 | ## Installation
|
9 |
|
10 | ```bash
|
11 | $ yarn add @shopify/dates
|
12 | ```
|
13 |
|
14 | ## Usage
|
15 |
|
16 | This library exports helpers that allow apps to easily work with dates and timezones.
|
17 | Optional time zone parameters that are omitted are inferred as local.
|
18 |
|
19 | ### `applyTimeZoneOffset`
|
20 |
|
21 | Takes in a date object and two optional time zone string parameters. Returns a new date object with the offset between the time zones added to it.
|
22 |
|
23 | ```ts
|
24 | import {applyTimeZoneOffset} from '@shopify/dates';
|
25 |
|
26 | const date = new Date('2018-06-01Z14:00');
|
27 | const timeZone1 = 'Australia/Perth';
|
28 | const timeZone2 = 'America/Toronto';
|
29 |
|
30 | const newDate = applyTimeZoneOffset(date, timeZone1, timeZone2);
|
31 | ```
|
32 |
|
33 | ### `formatDate`
|
34 |
|
35 | Takes in a date object and two additional parameters, the locale and an optional options object. Returns a new date string with the applied locale and options.
|
36 |
|
37 | ```ts
|
38 | import {formatDate} from '@shopify/dates';
|
39 |
|
40 | const date = new Date('2020-02-18Z14:00');
|
41 | const locales = 'en';
|
42 | const options = {
|
43 | timeZone: 'America/New_York',
|
44 | hour: 'numeric',
|
45 | };
|
46 |
|
47 | const newDate = formatDate(date, locales, options); // 9 AM
|
48 | ```
|
49 |
|
50 | ### `getDateTimeParts`
|
51 |
|
52 | Takes in a date object and an optional time zone string parameter. Returns an object with functions to get the year, month, day, weekday, hour, minute and second of the provided date.
|
53 |
|
54 | ```ts
|
55 | import {getDateTimeParts} from '@shopify/dates';
|
56 |
|
57 | const date = new Date('2018-06-01Z14:00');
|
58 | const timeZone = 'Australia/Perth';
|
59 |
|
60 | const dateTimeParts = getDateTimeParts(date, timeZone);
|
61 |
|
62 | const year = dateTimeParts.year();
|
63 | const month = dateTimeParts.month();
|
64 | const day = dateTimeParts.day();
|
65 | const weekday = dateTimeParts.weekday();
|
66 | const hour = dateTimeParts.hour();
|
67 | const minute = dateTimeParts.minute();
|
68 | const second = dateTimeParts.second();
|
69 | ```
|
70 |
|
71 | ### `getTimeZoneOffset`
|
72 |
|
73 | Takes in a date object and two optional time zone string parameters. Returns a number representing the offset between the two provided time zones in minutes.
|
74 |
|
75 | ```ts
|
76 | import {getTimeZoneOffset} from '@shopify/dates';
|
77 |
|
78 | const date = new Date('2018-06-01Z14:00');
|
79 | const timeZone1 = 'America/Toronto';
|
80 | const timeZone2 = 'Australia/Perth';
|
81 |
|
82 | const timeZoneOffset = getTimeZoneOffset(date, timeZone1, timeZone2);
|
83 | ```
|
84 |
|
85 | ### `isFutureDate`
|
86 |
|
87 | Takes in a date object and an optional now date object to compare against (defaulting to a new date object). Returns a boolean indicating whether or not the first date is in the future.
|
88 |
|
89 | ```ts
|
90 | import {isFutureDate} from '@shopify/dates';
|
91 |
|
92 | const now = new Date('2018-01-01Z00:00');
|
93 | const date = new Date('2018-01-02Z23:59');
|
94 |
|
95 | const futureDay = isFutureDate(date, now);
|
96 | ```
|
97 |
|
98 | ### `isLessThanOneDayAgo`
|
99 |
|
100 | Takes in a date object and an optional "now" date object (that defaults to `new Date()`). Returns a boolean indicating whether or not the date is less than one day before the "now" date.
|
101 |
|
102 | ```ts
|
103 | import {isLessThanOneDayAgo} from '@shopify/dates';
|
104 |
|
105 | const moreThanOneDayAgo = new Date('2018-01-01Z00:00');
|
106 | const lessThanOneDayAgo = new Date(Date.now() - 23 * TimeUnit.Hour);
|
107 |
|
108 | isLessThanOneDayAgo(moreThanOneDayAgo); // false
|
109 | isLessThanOneDayAgo(lessThanOneDayAgo); // true
|
110 | ```
|
111 |
|
112 | ### `isLessThanOneHourAgo`
|
113 |
|
114 | Takes in a date object and an optional "now" date object (that defaults to `new Date()`). Returns a boolean indicating whether or not the date is less than one hour before the "now" date.
|
115 |
|
116 | ```ts
|
117 | import {isLessThanOneHourAgo} from '@shopify/dates';
|
118 |
|
119 | const moreThanOneHourAgo = new Date('2018-01-01Z00:00');
|
120 | const lessThanOneHourAgo = new Date(Date.now() - 59 * TimeUnit.Minute);
|
121 |
|
122 | isLessThanOneHourAgo(moreThanOneHourAgo); // false
|
123 | isLessThanOneHourAgo(lessThanOneHourAgo); // true
|
124 | ```
|
125 |
|
126 | ### `isLessThanOneMinuteAgo`
|
127 |
|
128 | Takes in a date object and an optional "now" date object (that defaults to `new Date()`). Returns a boolean indicating whether or not the date is less than one minute before the "now" date.
|
129 |
|
130 | ```ts
|
131 | import {isLessThanOneMinuteAgo} from '@shopify/dates';
|
132 |
|
133 | const moreThanOneMinuteAgo = new Date('2018-01-01Z00:00');
|
134 | const lessThanOneMinuteAgo = new Date(Date.now() - 59 * TimeUnit.Second);
|
135 |
|
136 | isLessThanOneMinuteAgo(moreThanOneMinuteAgo); // false
|
137 | isLessThanOneMinuteAgo(lessThanOneMinuteAgo); // true
|
138 | ```
|
139 |
|
140 | ### `isLessThanOneWeekAgo`
|
141 |
|
142 | Takes in a date object and an optional "now" date object (that defaults to `new Date()`). Returns a boolean indicating whether or not the date is less than one week before the "now" date.
|
143 |
|
144 | ```ts
|
145 | import {isLessThanOneWeekAgo} from '@shopify/dates';
|
146 |
|
147 | const moreThanOneWeekAgo = new Date('2018-01-01Z00:00');
|
148 | const lessThanOneWeekAgo = new Date(Date.now() - 6 * TimeUnit.Day);
|
149 |
|
150 | isLessThanOneWeekAgo(moreThanOneWeekAgo); // false
|
151 | isLessThanOneWeekAgo(lessThanOneWeekAgo); // true
|
152 | ```
|
153 |
|
154 | ### `isLessThanOneYearAgo`
|
155 |
|
156 | Takes in a date object and an optional "now" date object (that defaults to `new Date()`). Returns a boolean indicating whether or not the date is less than one year before the "now" date.
|
157 |
|
158 | ```ts
|
159 | import {isLessThanOneYearAgo} from '@shopify/dates';
|
160 |
|
161 | const moreThanOneYearAgo = new Date('2018-01-01Z00:00');
|
162 | const lessThanOneYearAgo = new Date(Date.now() - 51 * TimeUnit.Week);
|
163 |
|
164 | isLessThanOneYearAgo(moreThanOneYearAgo); // false
|
165 | isLessThanOneYearAgo(lessThanOneYearAgo); // true
|
166 | ```
|
167 |
|
168 | ### `isSameDay`
|
169 |
|
170 | Takes in two date objects and an optional time zone string parameter. Returns a boolean indicating whether or not these two dates are in the same day.
|
171 |
|
172 | ```ts
|
173 | import {isSameDay} from '@shopify/dates';
|
174 |
|
175 | const date1 = '2018-01-01Z00:00';
|
176 | const date2 = '2018-01-02Z23:59';
|
177 | const timeZone = 'America/Toronto';
|
178 |
|
179 | const sameDay = isSameDay(date1, date2, timeZone);
|
180 | ```
|
181 |
|
182 | ### `isSameMonth`
|
183 |
|
184 | Takes in two date objects and an optional time zone string parameter. Returns a boolean indicating whether or not these two dates are in the same month.
|
185 |
|
186 | ```ts
|
187 | import {isSameMonth} from '@shopify/dates';
|
188 |
|
189 | const date1 = '2018-01-01Z00:00';
|
190 | const date2 = '2018-01-02Z23:59';
|
191 | const timeZone = 'America/Toronto';
|
192 |
|
193 | const sameMonth = isSameMonth(date1, date2, timeZone);
|
194 | ```
|
195 |
|
196 | ### `isSameYear`
|
197 |
|
198 | Takes in two date objects and an optional time zone string parameter. Returns a boolean indicating whether or not these two dates are in the same year.
|
199 |
|
200 | ```ts
|
201 | import {isSameYear} from '@shopify/dates';
|
202 |
|
203 | const date1 = '2018-01-01Z00:00';
|
204 | const date2 = '2018-01-02Z23:59';
|
205 | const timeZone = 'America/Toronto';
|
206 |
|
207 | const sameYear = isSameYear(date1, date2, timeZone);
|
208 | ```
|
209 |
|
210 | ### `isToday`
|
211 |
|
212 | Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is today.
|
213 |
|
214 | ```ts
|
215 | import {isToday} from '@shopify/dates';
|
216 |
|
217 | const date = '2018-01-01Z00:00';
|
218 | const timeZone = 'America/Toronto';
|
219 |
|
220 | const today = isToday(date, timeZone);
|
221 | ```
|
222 |
|
223 | ### `isTomorrow`
|
224 |
|
225 | Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is tomorrow.
|
226 |
|
227 | ```ts
|
228 | import {isTomorrow} from '@shopify/dates';
|
229 |
|
230 | const date = '2018-01-01Z00:00';
|
231 | const timeZone = 'America/Toronto';
|
232 |
|
233 | const tomorrow = isTomorrow(date, timeZone);
|
234 | ```
|
235 |
|
236 | ### `isYesterday`
|
237 |
|
238 | Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is yesterday.
|
239 |
|
240 | ```ts
|
241 | import {isYesterday} from '@shopify/dates';
|
242 |
|
243 | const date = '2018-01-01Z00:00';
|
244 | const timeZone = 'America/Toronto';
|
245 |
|
246 | const yesterday = isYesterday(date, timeZone);
|
247 | ```
|
248 |
|
249 | ### `mapDeprecatedTimezones`
|
250 |
|
251 | Takes in a time zone string parameter. Returns a time zone string corresponding to the equivalent, non-deprecated time zone string.
|
252 |
|
253 | ```ts
|
254 | import {mapDeprecatedTimezones} from '@shopify/dates';
|
255 |
|
256 | const deprecatedTimeZone = 'Cuba';
|
257 | const correctTimeZone = mapDeprecatedTimezones(deprecatedTimeZone); // In this case, returns 'America/Havana'
|
258 | ```
|
259 |
|
260 | ### `parseDateString`
|
261 |
|
262 | Takes in a date string and an optional time zone string parameter. Returns a date object with the format '2018-05-28T12:30:00+00:00' (yyyy-mm-ddThh:mm:ss+00:00, where '+00:00' represents the time zone offset)
|
263 |
|
264 | ```ts
|
265 | import {parseDateString} from '@shopify/dates';
|
266 |
|
267 | const date = '2018-01-01Z00:00';
|
268 | const timeZone = 'UTC';
|
269 |
|
270 | const parsed = parseDateString(date, timeZone);
|
271 | ```
|
272 |
|
273 | ### `parseDateStringParts`
|
274 |
|
275 | Takes in a date string. Returns parsed parts from that date string.
|
276 |
|
277 | ```ts
|
278 | import {parseDateStringParts} from '@shopify/dates';
|
279 |
|
280 | const date = '2018-05-28T12:30:00.123+05:30';
|
281 |
|
282 | const {
|
283 | year,
|
284 | month,
|
285 | day,
|
286 | hour,
|
287 | minute,
|
288 | second,
|
289 | millisecond,
|
290 | timeZoneOffset,
|
291 | sign,
|
292 | timeZoneHour,
|
293 | timeZoneMinute,
|
294 | } = parseDateStringParts(date);
|
295 | ```
|
296 |
|
297 | ### `unapplyTimeZoneOffset`
|
298 |
|
299 | Takes in a date object and two optional time zone string parameters. Returns a new date object with the offset between the time zones subtracted from it.
|
300 |
|
301 | ```ts
|
302 | import {unapplyTimeZoneOffset} from '@shopify/dates';
|
303 |
|
304 | const date = new Date('2018-06-01Z14:00');
|
305 | const timeZone = 'Australia/Perth';
|
306 |
|
307 | const newDate = unapplyTimeZoneOffset(date, offset);
|
308 | ```
|