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