How to make operations with Dates/Hours in Javascript?

前端 未结 3 1285
小鲜肉
小鲜肉 2021-01-28 14:03

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

3条回答
  •  悲哀的现实
    2021-01-28 14:38

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

提交回复
热议问题