Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript date subtraction

I am looking for a way to do proper subtraction between two javascript Date objects and get the day delta.

This is my approach but it fails for todays date as an input:

<script type="text/javascript">
function getDayDelta(incomingYear,incomingMonth,incomingDay){
var incomingDate = new Date(incomingYear,incomingMonth,incomingDay);
var today = new Date();

var delta = incomingDate - today;
var resultDate = new Date(delta);
return resultDate.getDate();
}
//works for the future dates:
alert(getDayDelta(2009,9,10));
alert(getDayDelta(2009,8,19));

//fails for the today as input, as expected 0 delta,instead gives 31:
alert(getDayDelta(2009,8,18));
</script>

What would be a better approach for this ?

like image 789
Hellnar Avatar asked Aug 18 '09 17:08

Hellnar


People also ask

How do you minus days from date in JS?

To subtract days to a JavaScript Date object, use the setDate() method. Under that, get the current days and subtract days. JavaScript date setDate() method sets the day of the month for a specified date according to local time.

How do you subtract days from a date?

Add or subtract days from a date Enter your due dates in column A. Enter the number of days to add or subtract in column B. You can enter a negative number to subtract days from your start date, and a positive number to add to your date. In cell C2, enter =A2+B2, and copy down as needed.

How do you subtract a value in JavaScript?

The subtraction operator ( - ) subtracts the two operands, producing their difference.

How do you subtract dates in TypeScript?

To calculate the time between 2 dates in TypeScript, call the getTime() method on both dates when subtracting, e.g. date2. getTime() - date1. getTime() .


3 Answers

The month number in the Date constructor function is zero based, you should substract one, and I think that is simplier to calculate the delta using the timestamp:

function getDayDelta(incomingYear,incomingMonth,incomingDay){
  var incomingDate = new Date(incomingYear,incomingMonth-1,incomingDay),
      today = new Date(), delta;
  // EDIT: Set time portion of date to 0:00:00.000
  // to match time portion of 'incomingDate'
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);
  today.setMilliseconds(0);

  // Remove the time offset of the current date
  today.setHours(0);
  today.setMinutes(0);

  delta = incomingDate - today;

  return Math.round(delta / 1000 / 60 / 60/ 24);
}


getDayDelta(2008,8,18); // -365
getDayDelta(2009,8,18); // 0
getDayDelta(2009,9,18); // 31
like image 194
Christian C. Salvadó Avatar answered Oct 13 '22 23:10

Christian C. Salvadó


(2009,8,18) is NOT August 18th. It's September 18th.

like image 24
Vijay Dev Avatar answered Oct 14 '22 00:10

Vijay Dev


You could call getTime() on each date object, then subtract the later from the earlier. This would give you the number of milliseconds difference between the two objects. From there, it's easy to get to days.

A couple of hiccups to watch for, though: 1) daylight savings time, and 2) ensuring your times are coming from the same time zone.

like image 45
Robert J. Walker Avatar answered Oct 13 '22 23:10

Robert J. Walker