Skip to content

Commit 57a06fd

Browse files
committed
Add Easy Date Comparison With DayJS as a javascript til
1 parent 19c01dd commit 57a06fd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_718 TILs and counting..._
13+
_719 TILs and counting..._
1414

1515
---
1616

@@ -247,6 +247,7 @@ _718 TILs and counting..._
247247
- [Default And Named Exports From The Same Module](javascript/default-and-named-exports-from-the-same-module.md)
248248
- [Destructuring The Rest Of An Array](javascript/destructuring-the-rest-of-an-array.md)
249249
- [Enable ES7 Transforms With react-rails](javascript/enable-es7-transforms-with-react-rails.md)
250+
- [Easy Date Comparison With DayJS](javascript/easy-date-comparison-with-dayjs.md)
250251
- [Expand Emojis With The Spread Operator](javascript/expand-emojis-with-the-spread-operator.md)
251252
- [Fill An Input With A Ton Of Text](javascript/fill-an-input-with-a-ton-of-text.md)
252253
- [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Easy Date Comparison With DayJS
2+
3+
Let's say my application fetches dates from the server which come back in
4+
string form as `"YYYY-MM-DD"` and I'd like to know if those dates already
5+
passed. This can be done easily by wrapping dates in
6+
[DayJS](https://github.com/iamkun/dayjs) and using its comparison functions.
7+
8+
```javascript
9+
import dayjs from 'dayjs';
10+
11+
const today = dayjs(new Date());
12+
const pastDate = dayjs("2018-10-22");
13+
const futureDate = dayjs("2022-01-01");
14+
15+
console.log(pastDate.isBefore(today));
16+
// => true
17+
console.log(futureDate.isBefore(today));
18+
// => false
19+
```
20+
21+
The `dayjs()` function can be used to construct DayJS date objects from Date
22+
objects and strings. These can then be compared with functions like
23+
`isBefore()` and `isAfter()`.

0 commit comments

Comments
 (0)