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 fe
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.
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);
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));