Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 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

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 :: gsheet calculate next tuesday date 
Javascript :: getelementbyid without the <script 
Javascript :: POST http://localhost:3000/$(process.env.REACT_APP_API_URL)/auth/users/ 404 (Not Found) in react redux 
Javascript :: javascripts 
Javascript :: js video controls false 
Javascript :: async loop with mongoose 
Javascript :: map vs subscribe angular 
Javascript :: fabic js save and render 
Javascript :: how to store and get a single variable in local storage js 
Javascript :: for ... in ... 
Javascript :: react native carriage return 
Javascript :: formatDuration js 
Javascript :: VueJS - getting the last element of a split string array 
Javascript :: error-too-many-re-renders-react-limits-the-number-of-renders-to-prevent 
Javascript :: flash effect in react native 
Javascript :: JS uramy8e7jth6klryes8hrd8utduf6kgiyes48w7iy6rdjfcghe49u 
Javascript :: Arr::flatten() The Arr::flatten method flattens a multi-dimensional array into a single level array: 
Javascript :: how to trigger on Blur only when clicked outside parent component and not child component in react js 
Javascript :: react-native-error-check-the-render-method-of-app 
Javascript :: Switching words in a string using replace 
Javascript :: expressjs cors blocked mixed-content 
Javascript :: useeffrc 
Javascript :: send back text from get request in node.js 
Javascript :: isPalindrome 
Javascript :: get latest journal entry without html 
Javascript :: Javascript Ternary operator | Light/Dark Theme 
Javascript :: dangerously meaning 
Javascript :: discord.js message edit 
Javascript :: link js filt to html file 
Javascript :: seperate array by comma in vue 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =