Created
October 17, 2020 08:50
-
-
Save waptik/32aafcdbbaec275d4f8246272794ed8b to your computer and use it in GitHub Desktop.
Migrating momentjs to date-fns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
The idea here is to replace momentjs with date-fns by using the right api to get the same result | |
you can test it out in https://repl.it/@waptik/WryElasticNlp | |
**/ | |
var moment = require("moment") | |
var {formatRelative, subMonths, sub, isBefore} = require("date-fns"); | |
let newD; | |
const Subs = sub(1610208638 * 1000, { months: 1}) // using this seems to give me the correct answer | |
newD = formatRelative(new Date(), 1610208638 * 1000) | |
newD =subMonths(new Date(newD), 1) | |
newD = isBefore(newD, new Date()) | |
const newIsBefore = isBefore(Subs, new Date()) | |
console.log("Subs ==>", Subs) | |
console.log("newIsBefore ==>", newIsBefore) // returns `false` which is correct | |
console.log("DateFns ==>", newD)// returns `true` which is wrong | |
console.log("Moment ==>", moment(1610208638 * 1000).subtract(1, 'month').isBefore(new Date()))// returns `false` which is correct | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment