2

I have to subtract two hours passed by the user in the format 'HH: MM' and the result has to divide by the number of feeds on the day and set the exact hours that it should feed.

What the best way to get the difference between 2 hours and divide by the number of meals and get the correct interval to save?

1
  • 7
    Is there any library that does this - moment.js Commented Jun 4, 2019 at 16:51

3 Answers 3

3

In most browsers, Dates support the subtraction operator, which will return the number of milliseconds between them. Alternatively, you can get a timestamp (the number of milliseconds since the Unix Epoch) by calling .getTime(). This second option may be a bit safer and have broader support, though I haven't found an authoritative source or document to corroborate.

The result will be positive if the Date on the left is after the date on the right and negative if it is before. If you want the difference represented as a positive number either way then you can use Math.abs().

var x = new Date(2019, 0, 1);
var y = new Date(2019, 0, 1, 5, 45);

// NOTE: You haven't specified how to handle "negative" differences
var diff = Math.abs(y.getTime() - x.getTime());

// From here it's simple arithmetic:
var hours = Math.floor(diff / 3600000);
var minutes = Math.floor((diff % 3600000) / 60000);

// NOTE: Differences greater than 99:59 will be truncated!
console.log( ("00" + hours).slice(-2) + ":" + ("00" + minutes).slice(-2) )

As for the rest of your question:

the result has to divide by the number of feeds on the day and set the exact hours that it should feed.

I have no idea what this means. Hopefully the date logic is enough for you to get started.

Sign up to request clarification or add additional context in comments.

Comments

1

You simply cannot pass hours, you have to pass Date to know which one is larger than other.

 function diff_hours(dt2, dt1) 
 {

  var diff =(dt2.getTime() - dt1.getTime()) / 1000;
  diff /= (60 * 60);
  return Math.abs(Math.round(diff));

 }
var amount_of_meal = 8;
dt1 = new Date(2014,10,2);
dt2 = new Date(2014,10,3);
var result = (diff_hours(dt1, dt2))/amount_of_meal;


dt1 = new Date("October 13, 2014 08:11:00");
dt2 = new Date("October 13, 2014 11:13:00");
console.log(diff_hours(dt1, dt2));

2 Comments

but there is a detail. in this second example. returns 3. representing 3 hours difference from first date to second. How can I get this value and divide by the amount of meals (8 for example) and get the correct interval to save the dates. Do you understand?
changed the answer according to your need
-3

If you prefer to use a library, you can use momentJS => https://momentjs.com/docs/

But I dont think you need to.

If you want to subtract two hours from a certain date you can do it like this /

var x = new Date();
x = Date.parse(x);
x = x - 7200000;
x = new Date(x);

console.log(x);

3 Comments

What's this line for? x = Date.parse(x);
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
Ok... it just doesn't make sense in your example since the line var x = new Date(); guarantees that x is already a date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.