-1

My Question is how do I calculate how much hours and minutes will pass to reach specific time. I mean if we have time: 15:32, and I want to calculate how much hours is needed to 05:32 (at night), in this case should be 14 hours and 32mins.

As I don't really have that much of a knowledge and experience I haven't tried anything because I don't really know where to start.

const curDate = new Date().toLocaleTimeString();
console.log(curDate);

//I only know how to get the current date to string. But what next?
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
Mike
  • 49
  • 5
  • Possible duplicate of [Calculate difference between 2 timestamps using javascript](https://stackoverflow.com/questions/16767301/calculate-difference-between-2-timestamps-using-javascript) – Paolo Apr 05 '19 at 13:50
  • If you have two `Date` values in JS, you can subtract one from another which will yield the number of milliseconds between them. You can then convert that to whatever time based metric you want. – Aman Agnihotri Apr 05 '19 at 13:52
  • Think of each time as a new date. There are tons of date related tutorials all over the web – charlietfl Apr 05 '19 at 13:52
  • Well i dont need to calculate the difference between 2 dates. I just need to know how much hours and minutes is left to x time. – Mike Apr 05 '19 at 13:53
  • 1
    How many hours and minutes are between two times *is* the difference between two dates. –  Apr 05 '19 at 13:53
  • May not sound intuitive but a new date includes hours, minutes and seconds – charlietfl Apr 05 '19 at 13:55
  • @charlietfl I think OP is trying to find the hours and minutes until a certain time of the day is reached. i.e. How many hours until the time is 05:32 again (similar to an alarm). So i think you have wrongfully flagged the question as a duplicate – nick zoum Apr 05 '19 at 14:17
  • 1
    @nickzoum using Date is a simple way to do that. you even used it in your answer. Stackoverflow isn't a free code writing service, or a *"how to"* tutorial service and the duplicate was meant as a way to get started. This question could also have been closed for "too broad" – charlietfl Apr 05 '19 at 14:20
  • @charlietfl That is irrelevant, I am just saying that this question is not a duplicate of `Get difference between 2 dates` – nick zoum Apr 05 '19 at 14:31
  • 1
    @nickzoum then find a better duplicate and edit the duplicate links. I assure you many of them exist and this question is off topic for several reasons – charlietfl Apr 05 '19 at 14:33
  • @charlietfl Couldn't find one, Closest i got was [this](https://stackoverflow.com/questions/42737992/making-an-alarm-clock-with-date-time). To be honest I would also expect a question such as this would had been asked already, but I can't find it – nick zoum Apr 05 '19 at 15:35

1 Answers1

0

To get the hours and minutes until a certain time of the day is reached then you should do the following steps:

  1. Create a date object with the current date
  2. Create a date object with the hour and minutes you are searching for
  3. Get the difference between the hours of the two (if it's smaller than 0 then that means that time has passed for the day so look for the next day)
  4. Get the difference between the minutes of the two (if it's smaller than 0 then that minutes that the minutes have passed, so check for the next hour)

Since you are subtracting a date from another date you don't need to do any conversions (Local => UTC)

const curDate = new Date(); // Step 1
const night = new Date(0, 0, 0, 5, 32); // Step 2
var hours = night.getHours() - curDate.getHours(); // Step 3a
if (hours < 0) hours += 24; // Step 3b
var minutes = night.getMinutes() - curDate.getMinutes(); // Step 4a
if (minutes < 0) { // Step 4b
  hours -= 1;
  minutes += 60;
}
console.log(["Hours: ", hours, ", Minutes: ", minutes].join(""));

Here is an example to help you understand step 4:

  • 08:54 - 07:56
  • 08 - 07 hours, 54 - 56 minutes
  • 1 hour, -2 minutes
  • 1 hour -1 hour => -2 minutes + 60 minutes
  • 0 hours, 58 minutes
nick zoum
  • 7,216
  • 7
  • 36
  • 80