UNPKG

9.31 kBMarkdownView Raw
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
6Lightweight date operations library.
7
8## Installation
9
10```bash
11$ yarn add @shopify/dates
12```
13
14## Usage
15
16This library exports helpers that allow apps to easily work with dates and timezones.
17Optional time zone parameters that are omitted are inferred as local.
18
19### `applyTimeZoneOffset`
20
21Takes 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
24import {applyTimeZoneOffset} from '@shopify/dates';
25
26const date = new Date('2018-06-01Z14:00');
27const timeZone1 = 'Australia/Perth';
28const timeZone2 = 'America/Toronto';
29
30const newDate = applyTimeZoneOffset(date, timeZone1, timeZone2);
31```
32
33### `formatDate`
34
35Takes 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
38import {formatDate} from '@shopify/dates';
39
40const date = new Date('2020-02-18Z14:00');
41const locales = 'en';
42const options = {
43 timeZone: 'America/New_York',
44 hour: 'numeric',
45};
46
47const newDate = formatDate(date, locales, options); // 9 AM
48```
49
50### `getDateTimeParts`
51
52Takes 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
55import {getDateTimeParts} from '@shopify/dates';
56
57const date = new Date('2018-06-01Z14:00');
58const timeZone = 'Australia/Perth';
59
60const dateTimeParts = getDateTimeParts(date, timeZone);
61
62const year = dateTimeParts.year();
63const month = dateTimeParts.month();
64const day = dateTimeParts.day();
65const weekday = dateTimeParts.weekday();
66const hour = dateTimeParts.hour();
67const minute = dateTimeParts.minute();
68const second = dateTimeParts.second();
69```
70
71### `getTimeZoneOffset`
72
73Takes 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
76import {getTimeZoneOffset} from '@shopify/dates';
77
78const date = new Date('2018-06-01Z14:00');
79const timeZone1 = 'America/Toronto';
80const timeZone2 = 'Australia/Perth';
81
82const timeZoneOffset = getTimeZoneOffset(date, timeZone1, timeZone2);
83```
84
85### `isFutureDate`
86
87Takes 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
90import {isFutureDate} from '@shopify/dates';
91
92const now = new Date('2018-01-01Z00:00');
93const date = new Date('2018-01-02Z23:59');
94
95const futureDay = isFutureDate(date, now);
96```
97
98### `isLessThanOneDayAgo`
99
100Takes 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
103import {isLessThanOneDayAgo} from '@shopify/dates';
104
105const moreThanOneDayAgo = new Date('2018-01-01Z00:00');
106const lessThanOneDayAgo = new Date(Date.now() - 23 * TimeUnit.Hour);
107
108isLessThanOneDayAgo(moreThanOneDayAgo); // false
109isLessThanOneDayAgo(lessThanOneDayAgo); // true
110```
111
112### `isLessThanOneHourAgo`
113
114Takes 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
117import {isLessThanOneHourAgo} from '@shopify/dates';
118
119const moreThanOneHourAgo = new Date('2018-01-01Z00:00');
120const lessThanOneHourAgo = new Date(Date.now() - 59 * TimeUnit.Minute);
121
122isLessThanOneHourAgo(moreThanOneHourAgo); // false
123isLessThanOneHourAgo(lessThanOneHourAgo); // true
124```
125
126### `isLessThanOneMinuteAgo`
127
128Takes 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
131import {isLessThanOneMinuteAgo} from '@shopify/dates';
132
133const moreThanOneMinuteAgo = new Date('2018-01-01Z00:00');
134const lessThanOneMinuteAgo = new Date(Date.now() - 59 * TimeUnit.Second);
135
136isLessThanOneMinuteAgo(moreThanOneMinuteAgo); // false
137isLessThanOneMinuteAgo(lessThanOneMinuteAgo); // true
138```
139
140### `isLessThanOneWeekAgo`
141
142Takes 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
145import {isLessThanOneWeekAgo} from '@shopify/dates';
146
147const moreThanOneWeekAgo = new Date('2018-01-01Z00:00');
148const lessThanOneWeekAgo = new Date(Date.now() - 6 * TimeUnit.Day);
149
150isLessThanOneWeekAgo(moreThanOneWeekAgo); // false
151isLessThanOneWeekAgo(lessThanOneWeekAgo); // true
152```
153
154### `isLessThanOneYearAgo`
155
156Takes 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
159import {isLessThanOneYearAgo} from '@shopify/dates';
160
161const moreThanOneYearAgo = new Date('2018-01-01Z00:00');
162const lessThanOneYearAgo = new Date(Date.now() - 51 * TimeUnit.Week);
163
164isLessThanOneYearAgo(moreThanOneYearAgo); // false
165isLessThanOneYearAgo(lessThanOneYearAgo); // true
166```
167
168### `isSameDay`
169
170Takes 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
173import {isSameDay} from '@shopify/dates';
174
175const date1 = '2018-01-01Z00:00';
176const date2 = '2018-01-02Z23:59';
177const timeZone = 'America/Toronto';
178
179const sameDay = isSameDay(date1, date2, timeZone);
180```
181
182### `isSameMonth`
183
184Takes 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
187import {isSameMonth} from '@shopify/dates';
188
189const date1 = '2018-01-01Z00:00';
190const date2 = '2018-01-02Z23:59';
191const timeZone = 'America/Toronto';
192
193const sameMonth = isSameMonth(date1, date2, timeZone);
194```
195
196### `isSameYear`
197
198Takes 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
201import {isSameYear} from '@shopify/dates';
202
203const date1 = '2018-01-01Z00:00';
204const date2 = '2018-01-02Z23:59';
205const timeZone = 'America/Toronto';
206
207const sameYear = isSameYear(date1, date2, timeZone);
208```
209
210### `isToday`
211
212Takes 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
215import {isToday} from '@shopify/dates';
216
217const date = '2018-01-01Z00:00';
218const timeZone = 'America/Toronto';
219
220const today = isToday(date, timeZone);
221```
222
223### `isTomorrow`
224
225Takes 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
228import {isTomorrow} from '@shopify/dates';
229
230const date = '2018-01-01Z00:00';
231const timeZone = 'America/Toronto';
232
233const tomorrow = isTomorrow(date, timeZone);
234```
235
236### `isYesterday`
237
238Takes 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
241import {isYesterday} from '@shopify/dates';
242
243const date = '2018-01-01Z00:00';
244const timeZone = 'America/Toronto';
245
246const yesterday = isYesterday(date, timeZone);
247```
248
249### `mapDeprecatedTimezones`
250
251Takes in a time zone string parameter. Returns a time zone string corresponding to the equivalent, non-deprecated time zone string.
252
253```ts
254import {mapDeprecatedTimezones} from '@shopify/dates';
255
256const deprecatedTimeZone = 'Cuba';
257const correctTimeZone = mapDeprecatedTimezones(deprecatedTimeZone); // In this case, returns 'America/Havana'
258```
259
260### `parseDateString`
261
262Takes 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
265import {parseDateString} from '@shopify/dates';
266
267const date = '2018-01-01Z00:00';
268const timeZone = 'UTC';
269
270const parsed = parseDateString(date, timeZone);
271```
272
273### `parseDateStringParts`
274
275Takes in a date string. Returns parsed parts from that date string.
276
277```ts
278import {parseDateStringParts} from '@shopify/dates';
279
280const date = '2018-05-28T12:30:00.123+05:30';
281
282const {
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
299Takes 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
302import {unapplyTimeZoneOffset} from '@shopify/dates';
303
304const date = new Date('2018-06-01Z14:00');
305const timeZone = 'Australia/Perth';
306
307const newDate = unapplyTimeZoneOffset(date, offset);
308```