0

I have a javascript function which add a certain amount of hours to a provided date and return the new date as follows.

$scope.CalDate = function()
{
var preDate = "2019-06-20 21:30";
var formatDate = new Date(preDate );
var diffHours = 2.30;
var newDate = new Date(formatDate.getTime() + (diffHours*1000*60*60));
return newDate;
}

Actual answer should be Wed Jun 21 2019 00:00:00. But it returns the following answer. Thu Jun 20 2019 23:48:00. Why is it happening ?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
tharindu
  • 513
  • 6
  • 26
  • This might be helpful https://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object – Nishant Singh Jun 03 '19 at 06:58
  • It's because you're adding 2.3 hours, not 2 and a half. Your diff would have to be 2.5 – George Jun 03 '19 at 06:58
  • See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) Also `new Date("2019-06-20 21:30")` returns an invalid date in some implementations. – RobG Jun 03 '19 at 07:54

2 Answers2

2

You are adding 2.3 hours instead of 2.5 hours(2 and a half)

$scope.CalDate = function()
{
var preDate = "2019-06-20 21:30";
var formatDate = new Date(preDate );
var diffHours = 2.5;//Add 2 and a half hours
var newDate = new Date(formatDate.getTime() + (diffHours*1000*60*60));
return newDate;
}

To convert 2.3 to the correct multiplier, use Math.floor(2.3) + (2.3%1)/0.6

$scope.CalDate = function()
{
var preDate = "2019-06-20 21:30";
var formatDate = new Date(preDate );
var diffHours = 2.3;
var actualDiff = Math.floor(diffHours) + (diffHours % 1)/0.6


var newDate = new Date(formatDate.getTime() + (actualDiff*1000*60*60));
return newDate;
}
SoWhat
  • 5,564
  • 2
  • 28
  • 59
  • Im getting the diffHours by using hours and minutes. Therefore it gives as 2.3. What shall I do? – tharindu Jun 03 '19 at 07:01
  • @tharindu you can simply multiple it proportionately? – wentjun Jun 03 '19 at 07:02
  • Math.floor(2.3) + (2.3%1)/0.6 - replace 2.3 with the variable where you store – SoWhat Jun 03 '19 at 07:07
  • Note that Javascript floating point calculations [*aren't always accurate*](http://stackoverflow.com/questions/588004/is-floating-point-math-broken), so using decimal hours in stead of hrs, mins, secs may produce minor errors that give annoyingly inaccurate results. If `"2.3"` should be `"2:30"` then treat it as such, don't convert it to `2.5` and hope things are OK. – RobG Jun 03 '19 at 07:25
  • Its correct @tharindu It translates to 2.499 which is close enough to 2.5. If you're using a literal 2.3 change it to 2.5. If you are calculating the 2.3, please post code on how you're getting to that number – SoWhat Jun 03 '19 at 08:08
0

To add the hours and minutes to a date following method was used.

$scope.CalDate = function()
{
var preDate = "2019-06-20 21:30";
var formatDate = new Date(preDate );
var diffHours = 2.3;
formatDate.setHours(formatDate.getHours() + 2);
formateDate.setMinutes(formatDate.getMinutes() + 30);
return formateDate;
}
tharindu
  • 513
  • 6
  • 26