Skip to content

Instantly share code, notes, and snippets.

@Ambratolm
Created February 23, 2022 19:38
Show Gist options
  • Save Ambratolm/ddde53d36e65634009c4296f9f983962 to your computer and use it in GitHub Desktop.
Save Ambratolm/ddde53d36e65634009c4296f9f983962 to your computer and use it in GitHub Desktop.
Set of handy functions to convert time to milliseconds. Added to global object to use everywhere.
global.seconds = (seconds) => Number(seconds) * 1e3;
global.minutes = (minutes) => Number(minutes) * 6e4;
global.hours = (hours) => Number(hours) * 3.6e6;
global.days = (days) => Number(days) * 8.64e7;
global.weeks = (weeks) => Number(weeks) * 6.048e8;
global.months = (months) => Number(months) * 2.628e9;
global.years = (years) => Number(years) * 3.154e10;
global.decades = (decades) => Number(decades) * 3.154e11;
global.centuries = () => Number(centuries) * 3.154e12;
global.date = (miliseconds) => new Date(Number(miliseconds));
// Usage convenience example
const someDate = new Date("Wed Feb 23 2022 19:28:21 GMT+0000"); // Date Object
const someDateMs = someDate.getTime(); // Milliseconds
const laterDateMs = someDateMs + years(1) + months(2) + days(5) + minutes(1); // Milliseconds
const laterDate = date(laterDateMs); // Date Object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment