Skip to content

Instantly share code, notes, and snippets.

@tofikhidayatxyz
Created May 13, 2022 01:36
Show Gist options
  • Select an option

  • Save tofikhidayatxyz/3ac83734fe8d1d8bf6d05b685fe2a550 to your computer and use it in GitHub Desktop.

Select an option

Save tofikhidayatxyz/3ac83734fe8d1d8bf6d05b685fe2a550 to your computer and use it in GitHub Desktop.
Countdount Formula
const now = moment().utc();
const origin = moment(this.dropAt);
const diffDay = origin.diff(now, "days");
const diffHour = origin.diff(now, "hours");
const diffMinutes = origin.diff(now, "minutes");
const diffSeconds = origin.diff(now, "seconds");
let finalWeek = Math.floor(diffDay / 7);
let finalDay = diffDay - finalWeek * 7;
let finalHours = diffHour - diffDay * 24;
let finalMinutes = diffMinutes - diffHour * 60;
let finalSeconds = diffSeconds - diffMinutes * 60;
// remap and makesure not have 0
finalWeek = finalWeek < 0 ? 0 : finalWeek;
finalDay = finalDay < 0 ? 0 : finalDay;
finalHours = finalHours < 0 ? 0 : finalHours;
finalMinutes = finalMinutes < 0 ? 0 : finalMinutes;
finalSeconds = finalSeconds < 0 ? 0 : finalSeconds;
// set state
this.week = finalWeek;
this.day = finalDay;
this.hour = finalHours;
this.minute = finalMinutes;
this.second = finalSeconds;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment