0
\$\begingroup\$

I have 2 dates that I would like to have the difference between them in days and if less than a one day simply show "less than a day".

remainingTime() {
  // Scientific notation of 8.64e+7
  const DAY_IN_MILLISECONDS = 8.64 * (10 ** 7);

  const date1 = new Date('2019-02-28T01:22:06.671Z').getTime();
  const date2 = new Date('2019-02-28T04:01:06.671Z').getTime();

  const differenceInMS = date2 - date1;

  const days = Math.floor(differenceInMS / DAY_IN_MILLISECONDS);
  return (days > 1) ? `They have ${days} days` : (days === 1) ? `They have ${days} day` : `you have less than a day left`;
}

Is this way correct if localization is taken into account?

what exactly is the difference between getTime (), getMilliseconds () and getUTCMilliseconds ()?

\$\endgroup\$

closed as off-topic by Graipher, Vogel612 Mar 5 at 17:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ You can use Exponentiation number notation for numbers. eg 8.64 * 10 ** 7 === 8.64e7 or '8.64E7' also ** has precedence over * so you don't need the () around 10 ** 7 \$\endgroup\$ – Blindman67 Mar 5 at 14:47
  • 1
    \$\begingroup\$ What localization? Your code isn't internationalized. \$\endgroup\$ – 200_success Mar 5 at 15:19
  • \$\begingroup\$ Considering that you have hard-coded English strings, this is obviously not correct, taking localization into account... \$\endgroup\$ – Graipher Mar 5 at 16:41
  • \$\begingroup\$ I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. \$\endgroup\$ – Vogel612 Mar 5 at 17:21
2
\$\begingroup\$

Your code looks good to me. I think 1000 * 60 * 60 * 24 would be more intuitive than the scientific notation, but either way gets the job done.

The script doesn't actually take localization into account because Date's getTime method is not localized. To account for time zone, you'd want to get the "timezone offset" of the user's location. (Note that getTimezoneOffset returns a number of minutes, and that number isn't a property of the date object but just an indication of what timezone the code is running in.)

As for the other methods
- getTime returns the actual "timestamp", a big integer showing how many milliseconds have passed since the 60s ended in London
- getUTCMilliseconds is just the last three digits of that timestamp (ie how many milliseconds it's been since the last full second since the "epoch", as it's called.)
- Leaving out the UTC bit (as in getMilliseconds, or in any of the similar get methods, excepting getTime) makes javascript calculate the local version for you (and in the case of milliseconds, I'd be pretty surprised if it were different from the universal version.)

...You can see the whole buffet on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Date.prototype_Methods

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.