Skip to content

Instantly share code, notes, and snippets.

@WrathChaos
Last active December 19, 2022 14:08
Show Gist options
  • Save WrathChaos/13b5d12464eb8e509437d4924267e12b to your computer and use it in GitHub Desktop.
Save WrathChaos/13b5d12464eb8e509437d4924267e12b to your computer and use it in GitHub Desktop.
How to get the difference between two dates with MomentJS? Article: WIP
// These are our dynamic variables
const format = "MM/DD/YYYY HH:mm:ss"
const start = moment(1578240964000); // Input starting timestamp
const end = moment(1578244564000); // Input ending timestamp
// Main Difference Logic
const countDownStart = start.add(1, "second");
const then = moment(countDownStart).format(format);
const now = moment(end).format(format);
const ms = moment(now, format).diff(
  moment(then, format)
);
const duration = moment.duration(ms);
const formattedDifference = duration.format("d[d]  hh:mm:ss"); // Output formatting
console.log("Formatted Difference: ", formattedDifference); //  3d 14:05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment