Answers for "js get difference in days"

27

javascript difference between two dates

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
console.log(getDifferenceInDays(date1, date2));
console.log(getDifferenceInHours(date1, date2));
console.log(getDifferenceInMinutes(date1, date2));
console.log(getDifferenceInSeconds(date1, date2));

function getDifferenceInDays(date1, date2) {
  const diffInMs = Math.abs(date2 - date1);
  return diffInMs / (1000 * 60 * 60 * 24);
}

function getDifferenceInHours(date1, date2) {
  const diffInMs = Math.abs(date2 - date1);
  return diffInMs / (1000 * 60 * 60);
}

function getDifferenceInMinutes(date1, date2) {
  const diffInMs = Math.abs(date2 - date1);
  return diffInMs / (1000 * 60);
}

function getDifferenceInSeconds(date1, date2) {
  const diffInMs = Math.abs(date2 - date1);
  return diffInMs / 1000;
}
Posted by: Guest on January-21-2020
4

How do I get the number of days between two dates in JavaScript

let today = new Date().toISOString().slice(0, 10)

const startDate  = '2021-04-15';
const endDate    = today;

const diffInMs   = new Date(endDate) - new Date(startDate)
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);


alert( diffInDays  );
Posted by: Guest on August-07-2021
2

javascript difference between two dates in days

const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));

// Example
diffDays(new Date('2014-12-19'), new Date('2020-01-01'));   // 1839
Posted by: Guest on July-03-2020
1

how to calculate the number of days between two dates in javascript

function javascript (date1, date2) {
   let oned = 24 * 60 * 60 * 1000;
   return Math.ceil((date2 - date1) / oned);
}

function javascript(date1, date2) {
	return new Date(date2 - date1).getDate() - 1
}

function javascript(date1, date2) {
  return Math.ceil((date2 - date1) / 8.64e7);
}
Posted by: Guest on June-23-2020
-1

js get difference in days

const date1 = new Date('19/1/2021');
const date2 = new Date('20/1/2021');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(diffTime + " milliseconds");
console.log(diffDays + " days");
Posted by: Guest on January-20-2021

Code answers related to "js get difference in days"

Code answers related to "Javascript"

Browse Popular Code Answers by Language