Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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;
}
Comment

get the difference between two dates js

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(diffTime + " milliseconds");
console.log(diffDays + " days");
Comment

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
Comment

js calculate date difference

var DateDiff = {

    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000));
    },

    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000*7));
    },

    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();

        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },

    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}

var dString = "May, 20, 1984";

var d1 = new Date(dString);
var d2 = new Date();

document.write("<br />Number of <b>days</b> since "+dString+": "+DateDiff.inDays(d1, d2));
document.write("<br />Number of <b>weeks</b> since "+dString+": "+DateDiff.inWeeks(d1, d2));
document.write("<br />Number of <b>months</b> since "+dString+": "+DateDiff.inMonths(d1, d2));
document.write("<br />Number of <b>years</b> since "+dString+": "+DateDiff.inYears(d1, d2));
Comment

difference between two dates in js

var d1 = new Date("08/14/2020"); 
var d2 = new Date("09/14/2020"); 
  
var diff = d2.getTime() - d1.getTime(); 
  
var daydiff = diff / (1000 * 60 * 60 * 24); 
document.write(daydiff + " days" ); 
Comment

get all date between two dates in javascript

// Returns an array of dates between the two dates
function getDates (startDate, endDate) {
  const dates = []
  let currentDate = startDate
  const addDays = function (days) {
    const date = new Date(this.valueOf())
    date.setDate(date.getDate() + days)
    return date
  }
  while (currentDate <= endDate) {
    dates.push(currentDate)
    currentDate = addDays.call(currentDate, 1)
  }
  return dates
}

// Usage
const dates = getDates(new Date(2013, 10, 22), new Date(2013, 11, 25))
dates.forEach(function (date) {
  console.log(date)
})
Comment

check a date is between two dates in javascript

const dateCheck = (from, to, check) => {
    let fDate,lDate,cDate;
    fDate = Date.parse(from);
    lDate = Date.parse(to);
    cDate = Date.parse(check);
    if((cDate <= lDate && cDate >= fDate))  return true
    return false;
}
dateCheck("02/05/2021","02/09/2021","02/07/2021")
Comment

how to check if date is between two dates in javascript

if((check.getTime() <= to.getTime() && check.getTime() >= from.getTime()))      alert("date contained");
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript window resize event 
Javascript :: electronjs start with devtools enabled 
Javascript :: drupal 8 get page node 
Javascript :: pick random string from array javascript 
Javascript :: Error: It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function. 
Javascript :: react-native italics 
Javascript :: counter cdn 
Javascript :: get attribute value jquery href 
Javascript :: javascript remove spaces at the beginning of the end of the string 
Javascript :: js go to previous page 
Javascript :: es module __dirname alternative 
Javascript :: convert hexadecimal to decimal js 
Javascript :: jquery click function 
Javascript :: how to get value and key in a for of loop in js 
Javascript :: return json with jango 
Javascript :: on keyup jquery 
Javascript :: disable right click 
Javascript :: jquery extract number from string 
Javascript :: copy a file and paste with fs 
Javascript :: how to get the value of dropdown in jquery 
Javascript :: Get List of all files in a directory in Node.js 
Javascript :: check if date time string is invalid date js 
Javascript :: js fake await 
Javascript :: node.js gitignore 
Javascript :: how to run vue js project on different port 
Javascript :: brain.js cdn 
Javascript :: uppercase string in js 
Javascript :: js explode equivalent 
Javascript :: js get numbers only 
Javascript :: Get First Day and last day of week javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =