mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Easy Date Comparison With DayJS as a javascript til
This commit is contained in:
23
javascript/easy-date-comparison-with-dayjs.md
Normal file
23
javascript/easy-date-comparison-with-dayjs.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Easy Date Comparison With DayJS
|
||||
|
||||
Let's say my application fetches dates from the server which come back in
|
||||
string form as `"YYYY-MM-DD"` and I'd like to know if those dates already
|
||||
passed. This can be done easily by wrapping dates in
|
||||
[DayJS](https://github.com/iamkun/dayjs) and using its comparison functions.
|
||||
|
||||
```javascript
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
const today = dayjs(new Date());
|
||||
const pastDate = dayjs("2018-10-22");
|
||||
const futureDate = dayjs("2022-01-01");
|
||||
|
||||
console.log(pastDate.isBefore(today));
|
||||
// => true
|
||||
console.log(futureDate.isBefore(today));
|
||||
// => false
|
||||
```
|
||||
|
||||
The `dayjs()` function can be used to construct DayJS date objects from Date
|
||||
objects and strings. These can then be compared with functions like
|
||||
`isBefore()` and `isAfter()`.
|
||||
Reference in New Issue
Block a user