compare 2 dates

Hi,
I’m struggling with an apparently easy task : compare 2 dates with :

if (dt < dt1) {
...

It doesn’t work. I have been looking in some JS forums, but nothing works. My date variables are declared as Objects with new Date(). I tried :

if (dt.getTime() < dt1.getTime()) {

which returns a “getTime() is not a function” error message

if (new Date(dt).getTime() < new Date(dt1).getTime()) {

does not work either

Here is the code :

let tableRows = $w('#table1').rows;   
var dt = Date(tableRows[0]['dateTime'].valueOf());
var Year = dt.substring(11,15);
dt = new Date(tableRows[0]['dateHeure'].valueOf());
var dt1 = new Date(Year,8,1);
console.log('dt='+dt+' dt1='+new Date(Year,8,1));

if (dt.getTime() < dt1.getTime()) {
...

here are the actual dt and dt1 returned by console.log:
dt=Tue Oct 29 2019 19:15:00 GMT+0100 dt1=Tue Sep 01 2020 00:00:00 GMT+0200

Any idea ? Thanks for the help !

1 Like

Have you tried simply searching the forum for previous posts about similar as you would have found…
https://www.wix.com/corvid/forum/community-discussion/date-difference-in-date-picker
https://www.wix.com/corvid/forum/community-discussion/find-the-difference-in-hours-between-2-dates

Exactly

Yisrael’s example is spot on.

function difference(dateOne, dateTwo) {
    let startDate = new Date(dateOne);
    var startTimeStamp = (startDate).getTime();
    let endDate = new Date(dateTwo);
    var endTimeStamp = (endDate).getTime();
    var microSecondsDiff = Math.abs(startTimeStamp - endTimeStamp);
//24 hrs/day * 60 minutes/hour * 60 seconds/minute * 1000 msecs/second
    var daysDiff = Math.floor(microSecondsDiff / (1000 * 60 * 60 * 24));
    console.log("days diff: " + daysDiff);
}