Skip to content

Instantly share code, notes, and snippets.

@mattc41190
Created April 8, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattc41190/9a59fef9a66c4e0f230a6e3c1f81c755 to your computer and use it in GitHub Desktop.
Save mattc41190/9a59fef9a66c4e0f230a6e3c1f81c755 to your computer and use it in GitHub Desktop.
How do those dern date calculators work?
const calculateTimeLeft = (month, day) => {
const now = new Date()
const nowInt = Date.parse(now)
const year = new Date().getFullYear()
const toDate = new Date(`${month}/${day}/${year}`)
const toDateInt = Date.parse(toDate)
const diff = toDateInt - nowInt
console.log(diff);
if (diff > 0) {
// All complexity lives here... If you get this then you "get it"
return {
// To get days you divide the number of milliseconds until a date by the product of
// multiplying 1000 * 60 (60,000) (milliseconds in a minute)
// multiplying that number (60,000) by 60 again (60,000 * 60 == 3,600,000) (milliseconds in an hour)
// finally multiply that number (3,600,000) by 24 (3,600,000 * 24 == 86,400,000) (milliseconds in a day)
// Note: This is the formula for finding out number of milliseconds in a day
// In reverse you are multiplying the number of hours in a day (24) x (60) == number of minutes in a day
// Taking that result you multiple by 60 again to get number of seconds in a day
// Taking that result you multiply once more to get number of milliseconds in a day
// Ex: If the difference in milliseconds between two dates is: 16,451,228,000
// Then all you need to do is figure out how many times 86,400,000 (one day) goes into that difference as a whole number
// 16,451,228,000/86,400,000 == 190.407731481
// Finally, you use Math.floor to eliminate remainder (we take care of what's left in the hours section)
// Leaving you with: 190 (or 190 days!)
days: Math.floor(diff / (1000 * 60 * 60 * 24)),
// To get hours you do something only slightly fancier:
// Similar to our days work we start by getting the number of milliseconds in a hour (3,600,000)
// We then divide the difference in the two time (int value) by this number using the same number
// as before: 16,451,228,000 the equation looks like this: 16,451,228,000/3,600,000 == 4569.785555555555
// This will return the difference in hours as a float.
// NOTE: At this point we are taking the TOTAL number of hours,
// but RECALL that we already have a number of days.
// so we want to only get what's LEFT when we divide this number by 24. Modulo to the rescue!
// We then modulo this number by 24 (the number of hours in a day) 4569.785555555555 % 24 == 9.78555555555522
// and finally we floor this number to get the loveliest number 9.
// So if you've been following along we are at: 190 days, 9 hours.
// Note in this section the parentheses are important as mod comes before division normally so watch out!
hours: Math.floor((diff / (1000 * 60 * 60)) % 24),
// Minutes and seconds are more similar to each other but certainly different from hours and days so let's get started!
// We start by dividing diff (let's continue on with our example: 16,451,228,000) by 1000 to get the number in seconds only
// So our total number of seconds is equal to: 16451228000 / 1000 == 16,451,228
// Note: That seconds number is how many seconds in total between the two dates could be useful for engineering this solution in a different fashion by getting seconds first and working up instead of getting seconds inside EVERY step
// next we diving that number by 60 to get the total number of minutes as a float
// That equation: 16,451,228 / 60 = 274187.13333333336 (without the float that's 274,187) just for fun and confidence let's
// divide that number by 60 to see hours 274187 / 60 == 4569 (that number should look familiar see last step)
// So the total number of seconds between the two dates is: 274187.13333333336, but we want only what remains when we remove the total number of hours from that number. Modulo to the rescue! 274187.13333333336 % 60 == 47.1333333333605
// Finally we floor this number and are left with 47
// So if you've been following along we are at: 190 days, 9 hours, 47 minutes.
minutes: Math.floor((diff / 1000 / 60) % 60),
// Seconds are the simplest to grasp in this formila for getting the difference betweeen two time and that
// is because in all other steps we have isolated seconds FIRST and then by some means shaved off
// the other "parts" of the time, often by making a float and then chopping off the remainder knowing it
// will be handled in another portion of our a formula. It still, however must be covered so let's get into it!
// We start by again dividing by 1000 to get the number of seconds only between the two dates.
// 16,451,228,000 / 1000 == 16,451,228 we then take ONLY the remainder of dividing this number by 60 to ensure that
// we do not capture any seconds amount greater than 60 since that number was already covered in our minutes "capturer".
// Finally we floor the number to ensure we don't go MORE precise than seconds. 16,451,228 % 60 == 9
// So if you've been following along our final product is: 190 days, 9 hours, 47 minutes, and 9 seconds!
// WOW! We did it! Congrats!
seconds: Math.floor((diff / 1000) % 60)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment