Wednesday, February 15, 2023

javascript date comparison

Some interesting things about client-side javascript date comparison - 

  • if you want to compare with ==, you would need to prefix the variable with a + but > and < would work fine.

  • you can't just write
    if ( Date(datestring) < Date("2022-12-08 23:59:59" ) ) - you need to create new Date(string) explicitly, like
    var d1 = new Date("2022-12-07 23:59:59" );
    var d2 = new Date("2022-12-08 23:59:59" );
    var testdate = new Date(testdatestr);
    if ( testdate > d1 && testdate < d2 )

No comments:

Post a Comment