Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to Perform Date Comparison With the Date Object in JavaScript

const compareDates = (d1, d2) => {
  let date1 = new Date(d1).getTime();
  let date2 = new Date(d2).getTime();

  if (date1 < date2) {
    console.log(`${d1} is less than ${d2}`);
  } else if (date1 > date2) {
    console.log(`${d1} is greater than ${d2}`);
  } else {
    console.log(`Both dates are equal`);
  }
};

compareDates("06/21/2022", "07/28/2021");
compareDates("01/01/2001", "01/01/2001");
compareDates("11/01/2021", "02/01/2022");

// This will return:

// "06/21/2022 is greater than 07/28/2021"
// "Both dates are equal"
// "11/01/2021 is less than 02/01/2022"
Comment

string compare on date in js

var d1 = Date.parse("2012-11-01");
var d2 = Date.parse("2012-11-04");
if (d1 < d2) {
    alert ("Error!");
}
Comment

Compare two dates in Javascript

// We initialize the Date() object with the current date and time
const date1 = new Date();
// We initialize a past date
const date2 = new Date('2018-04-07 12:30:00');

// Let's see if the first date is equal, more recent or less recent than the second date
if (date1.getTime() === date2.getTime()) {
	console.log('The dates are equal');
}
else if (date1.getTime() > date2.getTime()) {
	console.log(date1.toString() + ' is more recent than ' + date2.toString());
}
else {
	console.log(date1.toString() + ' is less recent than ' + date2.toString());
}
Comment

compare date and time in js

var isLarger = new Date("2-11-2012 13:40:00") > new Date("01-11-2012 10:40:00");
Comment

date compare in js

var x = new Date('2013-05-23');
var y = new Date('2013-05-23');

// less than, greater than is fine:
x < y; => false
x > y; => false
x === y; => false, oops!

// anything involving '=' should use the '+' prefix
// it will then compare the dates' millisecond values
+x <= +y;  => true
+x >= +y;  => true
+x === +y; => true
Comment

compare date javascript

// solution is convert date to time by getTime()
start 		= startDate.getTime();
end 		= endDate.getTime();
current 	= date.getTime();

if (start <= current && current <= end) {
  // do something here
}
Comment

Compare two dates in javascript

var date1 = new Date();
var date2 = new Date();

if (date1.getTime() === date2.getTime()) { // 1605815698393 === 1605815698393 
    console.log("Dates are equal");
}
else {
    console.log("Dates are Not equal");
}
Comment

compare date value in javascript

Comparing two dates is as simple as

var differenceInMs = dateNewer - dateOlder;
So, convert the timestamps back into Date instances

var d1 = new Date('2013-08-02T10:09:08Z'), // 10:09 to
    d2 = new Date('2013-08-02T10:20:08Z'); // 10:20 is 11 mins
Get the difference

var diff = d2 - d1;
Format this as desired

if (diff > 60e3) console.log(
    Math.floor(diff / 60e3), 'minutes ago'
);
else console.log(
    Math.floor(diff / 1e3), 'seconds ago'
);
// 11 minutes ago
Comment

javascript compare dates

let d1 = new Date();
let d2 = new Date();
// can use >, <, <=, <=
d1 > d2
d1 >= d2
// == won't work so can use this:
(d1 >= d2) && (d2 >= d1)
Comment

javascript compare dates

let myDate = new Date("January 13, 2021 12:00:00");
let yourDate = new Date("January 13, 2021 15:00:00");

if (myDate < yourDate) {
  console.log("myDate is less than yourDate"); // will be printed
}
if (myDate > yourDate) {
  console.log("myDate is greater than yourDate");
}
Comment

How to compare two different date formats in javascript

const d1 = new Date("Mon Jan 3 2022 16:38:58 GMT+0530 (India Standard Time)") 
const d2 = new Date("2022-01-03T18:30:00Z")
console.log(d1)
console.log(d2)

if (d1 < d2) console.log("Date 1 is earlier than d2")

// To find hh:mm:ss difference for the same day we can do this. 
console.log(new Date(d2-d1).toISOString().substr(11, 8))
Comment

compare two dates in javascript

a = new Date(1995,11,17);
b = new Date(1995,11,17);

a.getTime() === b.getTime() // prints true
Comment

PREVIOUS NEXT
Code Example
Javascript :: js regrex 
Javascript :: jquery traversing methods 
Javascript :: promises in javascript 
Javascript :: vue js override component css 
Javascript :: what is my version of linux mint 
Javascript :: clean-webpack-plugin clearing dist folder 
Javascript :: jquery accordion toggle close open 
Javascript :: UnhandledPromiseRejectionWarning 
Javascript :: frames[i] javascript 
Javascript :: nodelist to array 
Javascript :: js value to boolean 
Javascript :: fs readfile promise 
Javascript :: create chart in excel using javascript 
Javascript :: using server passed values and client js together in ejs 
Javascript :: onchange vue 
Javascript :: why node_modules are not installed anymore 
Javascript :: json object in html page 
Javascript :: set visible vue 
Javascript :: js get variable from url 
Javascript :: longest word in a string 
Javascript :: google analytics nextjs 
Javascript :: datepicker range npm reactjs 
Javascript :: React closing a dropdown when click outside 
Javascript :: what is js 
Javascript :: send data from form to another page angular 
Javascript :: onclick call function react 
Javascript :: run only one test cypress 
Javascript :: page scrolling react js 
Javascript :: how to set three js canvas width 100% 
Javascript :: while loop javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =