Last active
October 27, 2019 02:48
-
-
Save waptik/4db3603672a0126ff881d878f107eaa5 to your computer and use it in GitHub Desktop.
This uses zeit's ms to convert relation time (eg. 4 days ago) to convert to to preferred time format `10/23/19` using momentjs' luxon
This file contains 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
var { DateTime } = require("luxon") | |
var ms = require("ms") | |
/** | |
* this takes a relative time `xxx xxx ago` and returns `xxx xxx` | |
**/ | |
function stripAgo(date) { | |
const trime = date.split(" "); | |
// console.log("trime ==> ", trime); | |
return trime[2] === "ago" ? `${trime[0]} ${trime[1]}` : date; | |
} | |
var n18 = ms(stripAgo("3 days ago")) // send 3 days to `ms` | |
var dateObj = new Date(); // current date time | |
var d = dateObj.getTime() - n18 // substract preferred time from current datetime | |
DateTime.fromMillis(d).toFormat("LL/dd/yy"); // format date to `MM/DD/YY` eg. `10/23/19` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
working demo at https://codesandbox.io/s/kind-gould-cxhmf or https://cxhmf.sse.codesandbox.io/