UNPKG

10.9 kBMarkdownView Raw
1# `@shopify/dates`
2
3[![Build Status](https://github.com/Shopify/quilt/workflows/Node-CI/badge.svg?branch=main)](https://github.com/Shopify/quilt/actions?query=workflow%3ANode-CI)
4[![Build Status](https://github.com/Shopify/quilt/workflows/Ruby-CI/badge.svg?branch=main)](https://github.com/Shopify/quilt/actions?query=workflow%3ARuby-CI)
5[![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)
6
7Lightweight date operations library.
8
9## Installation
10
11```bash
12$ yarn add @shopify/dates
13```
14
15## Usage
16
17This library exports helpers that allow apps to easily work with dates and timezones.
18Optional time zone parameters that are omitted are inferred as local.
19
20### `applyTimeZoneOffset`
21
22Takes 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. We can also assume the passed date is in the first time zone and we want to calculate it in the second time zone.
23
24```ts
25import {applyTimeZoneOffset} from '@shopify/dates';
26
27const date = new Date('2018-06-01Z14:00');
28const timeZone1 = 'Australia/Perth';
29const timeZone2 = 'America/Toronto';
30
31const newDate = applyTimeZoneOffset(date, timeZone1, timeZone2); //'2018-06-01T02:00:00.000Z'
32```
33
34### `formatDate`
35
36Takes 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.
37
38```ts
39import {formatDate} from '@shopify/dates';
40
41const date = new Date('2020-02-18Z14:00');
42const locales = 'en';
43const options = {
44 timeZone: 'America/New_York',
45 hour: 'numeric',
46};
47
48const newDate = formatDate(date, locales, options); // 9 AM
49```
50
51### `getDateTimeParts`
52
53Takes 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.
54
55```ts
56import {getDateTimeParts} from '@shopify/dates';
57
58const date = new Date('2018-06-01Z14:00');
59const timeZone = 'Australia/Perth';
60
61const dateTimeParts = getDateTimeParts(date, timeZone);
62
63const year = dateTimeParts.year();
64const month = dateTimeParts.month();
65const day = dateTimeParts.day();
66const weekday = dateTimeParts.weekday();
67const hour = dateTimeParts.hour();
68const minute = dateTimeParts.minute();
69const second = dateTimeParts.second();
70```
71
72### `getTimeZoneOffset`
73
74Takes 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.
75
76```ts
77import {getTimeZoneOffset} from '@shopify/dates';
78
79const date = new Date('2018-06-01Z14:00');
80const timeZone1 = 'America/Toronto';
81const timeZone2 = 'Australia/Perth';
82
83const timeZoneOffset = getTimeZoneOffset(date, timeZone1, timeZone2);
84```
85
86### `isFutureDate`
87
88Takes 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.
89
90```ts
91import {isFutureDate} from '@shopify/dates';
92
93const now = new Date('2018-01-01Z00:00');
94const date = new Date('2018-01-02Z23:59');
95
96const futureDay = isFutureDate(date, now);
97```
98
99### `isLessThanOneDayAgo`
100
101Takes 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.
102
103```ts
104import {isLessThanOneDayAgo} from '@shopify/dates';
105
106const moreThanOneDayAgo = new Date('2018-01-01Z00:00');
107const lessThanOneDayAgo = new Date(Date.now() - 23 * TimeUnit.Hour);
108
109isLessThanOneDayAgo(moreThanOneDayAgo); // false
110isLessThanOneDayAgo(lessThanOneDayAgo); // true
111```
112
113### `isLessThanOneHourAgo`
114
115Takes 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.
116
117```ts
118import {isLessThanOneHourAgo} from '@shopify/dates';
119
120const moreThanOneHourAgo = new Date('2018-01-01Z00:00');
121const lessThanOneHourAgo = new Date(Date.now() - 59 * TimeUnit.Minute);
122
123isLessThanOneHourAgo(moreThanOneHourAgo); // false
124isLessThanOneHourAgo(lessThanOneHourAgo); // true
125```
126
127### `isLessThanOneMinuteAgo`
128
129Takes 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.
130
131```ts
132import {isLessThanOneMinuteAgo} from '@shopify/dates';
133
134const moreThanOneMinuteAgo = new Date('2018-01-01Z00:00');
135const lessThanOneMinuteAgo = new Date(Date.now() - 59 * TimeUnit.Second);
136
137isLessThanOneMinuteAgo(moreThanOneMinuteAgo); // false
138isLessThanOneMinuteAgo(lessThanOneMinuteAgo); // true
139```
140
141### `isLessThanOneWeekAgo`
142
143Takes 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.
144
145```ts
146import {isLessThanOneWeekAgo} from '@shopify/dates';
147
148const moreThanOneWeekAgo = new Date('2018-01-01Z00:00');
149const lessThanOneWeekAgo = new Date(Date.now() - 6 * TimeUnit.Day);
150
151isLessThanOneWeekAgo(moreThanOneWeekAgo); // false
152isLessThanOneWeekAgo(lessThanOneWeekAgo); // true
153```
154
155### `isLessThanOneWeekAway`
156
157Takes 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 after the "now" date.
158
159```ts
160import {isLessThanOneWeekAway} from '@shopify/dates';
161
162const moreThanOneWeekAway = new Date(Date.now() + 8 * TimeUnit.Day);
163const lessThanOneWeekAway = new Date(Date.now() + 6 * TimeUnit.Day);
164
165isLessThanOneWeekAway(moreThanOneWeekAway); // false
166isLessThanOneWeekAway(lessThanOneWeekAway); // true
167```
168
169### `isLessThanOneYearAgo`
170
171Takes 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.
172
173```ts
174import {isLessThanOneYearAgo} from '@shopify/dates';
175
176const moreThanOneYearAgo = new Date('2018-01-01Z00:00');
177const lessThanOneYearAgo = new Date(Date.now() - 51 * TimeUnit.Week);
178
179isLessThanOneYearAgo(moreThanOneYearAgo); // false
180isLessThanOneYearAgo(lessThanOneYearAgo); // true
181```
182
183### `isLessThanOneYearAway`
184
185Takes 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 after the "now" date.
186
187```ts
188import {isLessThanOneYearAway} from '@shopify/dates';
189
190const moreThanOneYearAway = new Date(Date.now() + 53 * TimeUnit.Week);
191const lessThanOneYearAway = new Date(Date.now() + 51 * TimeUnit.Week);
192
193isLessThanOneYearAway(moreThanOneYearAway); // false
194isLessThanOneYearAway(lessThanOneYearAway); // true
195```
196
197### `isSameDay`
198
199Takes 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.
200
201```ts
202import {isSameDay} from '@shopify/dates';
203
204const date1 = '2018-01-01Z00:00';
205const date2 = '2018-01-02Z23:59';
206const timeZone = 'America/Toronto';
207
208const sameDay = isSameDay(date1, date2, timeZone);
209```
210
211### `isSameMonth`
212
213Takes 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.
214
215```ts
216import {isSameMonth} from '@shopify/dates';
217
218const date1 = '2018-01-01Z00:00';
219const date2 = '2018-01-02Z23:59';
220const timeZone = 'America/Toronto';
221
222const sameMonth = isSameMonth(date1, date2, timeZone);
223```
224
225### `isSameYear`
226
227Takes 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.
228
229```ts
230import {isSameYear} from '@shopify/dates';
231
232const date1 = '2018-01-01Z00:00';
233const date2 = '2018-01-02Z23:59';
234const timeZone = 'America/Toronto';
235
236const sameYear = isSameYear(date1, date2, timeZone);
237```
238
239### `isToday`
240
241Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is today.
242
243```ts
244import {isToday} from '@shopify/dates';
245
246const date = '2018-01-01Z00:00';
247const timeZone = 'America/Toronto';
248
249const today = isToday(date, timeZone);
250```
251
252### `isTomorrow`
253
254Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is tomorrow.
255
256```ts
257import {isTomorrow} from '@shopify/dates';
258
259const date = '2018-01-01Z00:00';
260const timeZone = 'America/Toronto';
261
262const tomorrow = isTomorrow(date, timeZone);
263```
264
265### `isYesterday`
266
267Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is yesterday.
268
269```ts
270import {isYesterday} from '@shopify/dates';
271
272const date = '2018-01-01Z00:00';
273const timeZone = 'America/Toronto';
274
275const yesterday = isYesterday(date, timeZone);
276```
277
278### `mapDeprecatedTimezones`
279
280Takes in a time zone string parameter. Returns a time zone string corresponding to the equivalent, non-deprecated time zone string.
281
282```ts
283import {mapDeprecatedTimezones} from '@shopify/dates';
284
285const deprecatedTimeZone = 'Cuba';
286const correctTimeZone = mapDeprecatedTimezones(deprecatedTimeZone); // In this case, returns 'America/Havana'
287```
288
289### `parseDateString`
290
291Takes 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)
292
293```ts
294import {parseDateString} from '@shopify/dates';
295
296const date = '2018-01-01Z00:00';
297const timeZone = 'UTC';
298
299const parsed = parseDateString(date, timeZone);
300```
301
302### `parseDateStringParts`
303
304Takes in a date string. Returns parsed parts from that date string.
305
306```ts
307import {parseDateStringParts} from '@shopify/dates';
308
309const date = '2018-05-28T12:30:00.123+05:30';
310
311const {
312 year,
313 month,
314 day,
315 hour,
316 minute,
317 second,
318 millisecond,
319 timeZoneOffset,
320 sign,
321 timeZoneHour,
322 timeZoneMinute,
323} = parseDateStringParts(date);
324```
325
326### `unapplyTimeZoneOffset`
327
328Takes 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. We can also assume the passed date is in the second time zone and we want to calculate it back in the first time zone.
329
330```ts
331import {unapplyTimeZoneOffset} from '@shopify/dates';
332
333const date = new Date('2018-06-01Z14:00');
334const timeZone1 = 'Australia/Perth';
335const timeZone2 = 'America/Toronto';
336
337const newDate = unapplyTimeZoneOffset(date, timeZone1, timeZone2); //2018-06-02T02:00:00.000Z
338```