UNPKG

8.88 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### `getDateTimeParts`
34
35Takes 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.
36
37```ts
38import {getDateTimeParts} from '@shopify/dates';
39
40const date = new Date('2018-06-01Z14:00');
41const timeZone = 'Australia/Perth';
42
43const dateTimeParts = getDateTimeParts(date, timeZone);
44
45const year = dateTimeParts.year();
46const month = dateTimeParts.month();
47const day = dateTimeParts.day();
48const weekday = dateTimeParts.weekday();
49const hour = dateTimeParts.hour();
50const minute = dateTimeParts.minute();
51const second = dateTimeParts.second();
52```
53
54### `getTimeZoneOffset`
55
56Takes 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.
57
58```ts
59import {getTimeZoneOffset} from '@shopify/dates';
60
61const date = new Date('2018-06-01Z14:00');
62const timeZone1 = 'America/Toronto';
63const timeZone2 = 'Australia/Perth';
64
65const timeZoneOffset = getTimeZoneOffset(date, timeZone1, timeZone2);
66```
67
68### `isFutureDate`
69
70Takes 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.
71
72```ts
73import {isFutureDate} from '@shopify/dates';
74
75const now = new Date('2018-01-01Z00:00');
76const date = new Date('2018-01-02Z23:59');
77
78const futureDay = isFutureDate(date, now);
79```
80
81### `isLessThanOneDayAgo`
82
83Takes 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.
84
85```ts
86import {isLessThanOneDayAgo} from '@shopify/dates';
87
88const moreThanOneDayAgo = new Date('2018-01-01Z00:00');
89const lessThanOneDayAgo = new Date(Date.now() - 23 * TimeUnit.Hour);
90
91isLessThanOneDayAgo(moreThanOneDayAgo); // false
92isLessThanOneDayAgo(lessThanOneDayAgo); // true
93```
94
95### `isLessThanOneHourAgo`
96
97Takes 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.
98
99```ts
100import {isLessThanOneHourAgo} from '@shopify/dates';
101
102const moreThanOneHourAgo = new Date('2018-01-01Z00:00');
103const lessThanOneHourAgo = new Date(Date.now() - 59 * TimeUnit.Minute);
104
105isLessThanOneHourAgo(moreThanOneHourAgo); // false
106isLessThanOneHourAgo(lessThanOneHourAgo); // true
107```
108
109### `isLessThanOneMinuteAgo`
110
111Takes 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.
112
113```ts
114import {isLessThanOneMinuteAgo} from '@shopify/dates';
115
116const moreThanOneMinuteAgo = new Date('2018-01-01Z00:00');
117const lessThanOneMinuteAgo = new Date(Date.now() - 59 * TimeUnit.Second);
118
119isLessThanOneMinuteAgo(moreThanOneMinuteAgo); // false
120isLessThanOneMinuteAgo(lessThanOneMinuteAgo); // true
121```
122
123### `isLessThanOneWeekAgo`
124
125Takes 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.
126
127```ts
128import {isLessThanOneWeekAgo} from '@shopify/dates';
129
130const moreThanOneWeekAgo = new Date('2018-01-01Z00:00');
131const lessThanOneWeekAgo = new Date(Date.now() - 6 * TimeUnit.Day);
132
133isLessThanOneWeekAgo(moreThanOneWeekAgo); // false
134isLessThanOneWeekAgo(lessThanOneWeekAgo); // true
135```
136
137### `isLessThanOneYearAgo`
138
139Takes 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.
140
141```ts
142import {isLessThanOneYearAgo} from '@shopify/dates';
143
144const moreThanOneYearAgo = new Date('2018-01-01Z00:00');
145const lessThanOneYearAgo = new Date(Date.now() - 51 * TimeUnit.Week);
146
147isLessThanOneYearAgo(moreThanOneYearAgo); // false
148isLessThanOneYearAgo(lessThanOneYearAgo); // true
149```
150
151### `isSameDay`
152
153Takes 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.
154
155```ts
156import {isSameDay} from '@shopify/dates';
157
158const date1 = '2018-01-01Z00:00';
159const date2 = '2018-01-02Z23:59';
160const timeZone = 'America/Toronto';
161
162const sameDay = isSameDay(date1, date2, timeZone);
163```
164
165### `isSameMonth`
166
167Takes 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.
168
169```ts
170import {isSameMonth} from '@shopify/dates';
171
172const date1 = '2018-01-01Z00:00';
173const date2 = '2018-01-02Z23:59';
174const timeZone = 'America/Toronto';
175
176const sameMonth = isSameMonth(date1, date2, timeZone);
177```
178
179### `isSameYear`
180
181Takes 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.
182
183```ts
184import {isSameYear} from '@shopify/dates';
185
186const date1 = '2018-01-01Z00:00';
187const date2 = '2018-01-02Z23:59';
188const timeZone = 'America/Toronto';
189
190const sameYear = isSameYear(date1, date2, timeZone);
191```
192
193### `isToday`
194
195Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is today.
196
197```ts
198import {isToday} from '@shopify/dates';
199
200const date = '2018-01-01Z00:00';
201const timeZone = 'America/Toronto';
202
203const today = isToday(date, timeZone);
204```
205
206### `isTomorrow`
207
208Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is tomorrow.
209
210```ts
211import {isTomorrow} from '@shopify/dates';
212
213const date = '2018-01-01Z00:00';
214const timeZone = 'America/Toronto';
215
216const tomorrow = isTomorrow(date, timeZone);
217```
218
219### `isYesterday`
220
221Takes in a date object and an optional time zone string parameter. Returns a boolean indicating whether or not this date is yesterday.
222
223```ts
224import {isYesterday} from '@shopify/dates';
225
226const date = '2018-01-01Z00:00';
227const timeZone = 'America/Toronto';
228
229const yesterday = isYesterday(date, timeZone);
230```
231
232### `mapDeprecatedTimezones`
233
234Takes in a time zone string parameter. Returns a time zone string corresponding to the equivalent, non-deprecated time zone string.
235
236```ts
237import {mapDeprecatedTimezones} from '@shopify/dates';
238
239const deprecatedTimeZone = 'Cuba';
240const correctTimeZone = mapDeprecatedTimezones(deprecatedTimeZone); // In this case, returns 'America/Havana'
241```
242
243### `parseDateString`
244
245Takes 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)
246
247```ts
248import {parseDateString} from '@shopify/dates';
249
250const date = '2018-01-01Z00:00';
251const timeZone = 'UTC';
252
253const parsed = parseDateString(date, timeZone);
254```
255
256### `parseDateStringParts`
257
258Takes in a date string. Returns parsed parts from that date string.
259
260```ts
261import {parseDateStringParts} from '@shopify/dates';
262
263const date = '2018-05-28T12:30:00.123+05:30';
264
265const {
266 year,
267 month,
268 day,
269 hour,
270 minute,
271 second,
272 millisecond,
273 timeZoneOffset,
274 sign,
275 timeZoneHour,
276 timeZoneMinute,
277} = parseDateStringParts(date);
278```
279
280### `unapplyTimeZoneOffset`
281
282Takes 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.
283
284```ts
285import {unapplyTimeZoneOffset} from '@shopify/dates';
286
287const date = new Date('2018-06-01Z14:00');
288const timeZone = 'Australia/Perth';
289
290const newDate = unapplyTimeZoneOffset(date, offset);
291```