Skip to content

Instantly share code, notes, and snippets.

@waptik
Last active October 27, 2019 02:48
Show Gist options
  • Save waptik/4db3603672a0126ff881d878f107eaa5 to your computer and use it in GitHub Desktop.
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
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`
@waptik
Copy link
Author

waptik commented Oct 27, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment