File tree 2 files changed +25
-1
lines changed
2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
10
10
For a steady stream of TILs from a variety of rocketeers, checkout
11
11
[ til.hashrocket.com] ( https://til.hashrocket.com/ ) .
12
12
13
- _ 718 TILs and counting..._
13
+ _ 719 TILs and counting..._
14
14
15
15
---
16
16
@@ -247,6 +247,7 @@ _718 TILs and counting..._
247
247
- [ Default And Named Exports From The Same Module] ( javascript/default-and-named-exports-from-the-same-module.md )
248
248
- [ Destructuring The Rest Of An Array] ( javascript/destructuring-the-rest-of-an-array.md )
249
249
- [ 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 )
250
251
- [ Expand Emojis With The Spread Operator] ( javascript/expand-emojis-with-the-spread-operator.md )
251
252
- [ Fill An Input With A Ton Of Text] ( javascript/fill-an-input-with-a-ton-of-text.md )
252
253
- [ Freeze An Object, Sorta] ( javascript/freeze-an-object-sorta.md )
Original file line number Diff line number Diff line change
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() ` .
You can’t perform that action at this time.
0 commit comments