The Date object will do what you want - construct one for each date, then compare them using the >, <, <= or >=.
The ==, !=, ===, and !== operators require you to use date.getTime() as in
var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();
to be clear just checking for equality directly with the date objects won't work
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
I suggest you use drop-downs or some similar constrained form of date entry rather than text boxes, though, lest you find yourself in input validation hell.
For the curious, date.getTime() documentation:
Answer from moonshadow on Stack OverflowReturns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. (Negative values are returned for prior times.)
The Date object will do what you want - construct one for each date, then compare them using the >, <, <= or >=.
The ==, !=, ===, and !== operators require you to use date.getTime() as in
var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();
to be clear just checking for equality directly with the date objects won't work
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
I suggest you use drop-downs or some similar constrained form of date entry rather than text boxes, though, lest you find yourself in input validation hell.
For the curious, date.getTime() documentation:
Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. (Negative values are returned for prior times.)
Compare < and > just as usual, but anything involving == or === should use a + prefix. Like so:
const x = new Date('2013-05-23');
const y = new Date('2013-05-23');
// less than, greater than is fine:
console.log('x < y', x < y); // false
console.log('x > y', x > y); // false
console.log('x <= y', x <= y); // true
console.log('x >= y', x >= y); // true
console.log('x === y', x === y); // false, oops!
// anything involving '==' or '===' should use the '+' prefix
// it will then compare the dates' millisecond values
console.log('+x === +y', +x === +y); // true
Videos
Good day all,
I'm a network security engineer by trade however have a small amount of programming knowledge so i'm attempting to write some Javascript that takes a ticket displayed in a ticket queue and based on its follow-up date set by the aligned engineer, appears green, yellow or red.
The issue I face, is formatting/normalising the dates so I can conduct a comparison. I need to decide on my approach here as I know it can be done in different ways with varying degrees of difficulty.
Ideally I would format today's date into '02 Oct, 2023 - 11:00' format, however I also need to separate the time so I can do time comparisons for tickets that have intraday deadlines. I know I can use the split() function to achieve this and save the value I want as a variable - but still insure on how to actually compare the dates themselves and which way round to normalise would be easiest.
As can be seen below, the follow-up date from the ticketing system is stored as a title element.
<div class="ellipsis" data-test-id="date-cell" title="02 Oct, 2023 11:00">
02 Oct, 2023 11:00
</div>
For example, lets say today's date is '04/10/2023 10:30' and I need to compare that to '02 Oct, 2023 11:00' - how would you go about doing this?
Thanks in advance for your support, no doubt you can help a poor old netsec engineer save some time.