Forum Discussion

wpenfold's avatar
wpenfold
Contributor
5 years ago

How to determine if today is between 2 dates

A task I frequently need to do is determine if today is between 2 dates (without time). I’m wondering if anyone has found a more elegant way to do this? I’ve been converting the start and end dates from an oracle database, and the current date to strings, then comparing them. It’s ugly and I feel like there should be a better way?
Date.now().toLocaleDateString() >= $GZCNTRL_START_DATE.toString().substr(0,10) && Date.now().toLocaleDateString() <= $GZCNTRL_END_DATE.toString().substr(0,10)

4 Replies

    • wpenfold's avatar
      wpenfold
      Contributor

      Yes, but I didn’t see how I could use it. Now() returns date and time, and I need just date (so the last day in the range isn’t omitted), plus I had issues because the dates from Oracle are local date and that gives an error comparing to the Now() date --which must be UTC?

      • alchemiz's avatar
        alchemiz
        Contributor III

        Hi Wendy,

        When invoking date parse and format was not set, it will convert that into UTC, after parsing the 2 dates all of your dates are now set in UTC, removing time or setting it up as 00:00:00 you can invoke the .toLocaleDateString()… then do the date parse again to perform compare dates… see screenshot below