Sidebar

What day is it? How to use code wizard functions to compare dates in javascript?

0 votes
14.3K views
asked May 11, 2018 by rich-c-2789 (16,180 points)
edited May 11, 2018 by rich-c-2789

I need to check how old a past date is or how far in the future.  I need to answer these types of date questions:

  • Is it a within a day ago?  
  • Was it more than a day ago?  
  • Is it more than two weeks from today?  
  • Is it less than a month away?    

I see the isDateWithin(date, offset, dateUnit) function.  I think this will work to answer these type of date questions but how do the offset and dateUnit alter the behaviour?

1 Answer

+1 vote

Lets's start with the details provided in the code wizard:

Now let's move to some examples.  

First, let's demonstrate how the offset is used.  For all examples assume today is April 30th 2018.

Example 1 (using negative 1 for the offset):

qie.debug('Q: Is given date within one day prior to today?');

qie.debug("A: for '04/28/2018': " + 
    qie.isDateWithin('04/28/2018', -1, 'DAY'));
qie.debug("A: for '04/29/2018': " + 
    qie.isDateWithin('04/29/2018', -1, 'DAY'));
qie.debug("A: for '04/30/2018': " + 
    qie.isDateWithin('04/30/2018', -1, 'DAY'));
qie.debug("A: for '05/01/2018': " + 
    qie.isDateWithin('05/01/2018', -1, 'DAY'));
qie.debug("A: for '05/02/2018': " + 
    qie.isDateWithin('05/02/2018', -1, 'DAY'));

 Output:

Q: Is given date within one day prior to today?

A: for '04/28/2018': false
A: for '04/29/2018': true
A: for '04/30/2018': true
A: for '05/01/2018': false
A: for '05/02/2018': false

    
Example 2 (using positive 1 for the offset):

qie.debug('Q: Is given date within one day following today?');

qie.debug("A: for '04/28/2018': " + 
    qie.isDateWithin('04/28/2018', 1, 'DAY'));
qie.debug("A: for '04/29/2018': " + 
    qie.isDateWithin('04/29/2018', 1, 'DAY'));
qie.debug("A: for '04/30/2018': " + 
    qie.isDateWithin('04/30/2018', 1, 'DAY'));
qie.debug("A: for '05/01/2018': " + 
    qie.isDateWithin('05/01/2018', 1, 'DAY'));
qie.debug("A: for '05/02/2018': " + 
    qie.isDateWithin('05/02/2018', 1, 'DAY')); 

Output:

A: for '04/28/2018': false
A: for '04/29/2018': false
A: for '04/30/2018': true
A: for '05/01/2018': true
A: for '05/02/2018': false  

Now compare the output from both examples above to the table below for the DAY date unit.  

In the table above we can see that if we call this function again but change the date etc. like this "qie.isDateWithin('04/27/2018', -1, 'DAY');" that we can expect false as the result.

One more example:

Example 3 (using negative 3 for the offset):

qie.debug('Q: Is given date within 3 days prior to today?');

qie.debug("A: for '04/26/2018': " + 
    qie.isDateWithin('04/26/2018', -3, 'DAY'));
qie.debug("A: for '04/27/2018': " + 
    qie.isDateWithin('04/27/2018', -3, 'DAY'));   

Output:

A: for '04/26/2018': false 
A: for '04/27/2018': true

So, you can see how the range pushes out from today, either in the past or future, based on the offset. 

The dateUnit also changes the range as can be seen in the table below:   

See also:

What is today? Can I check to see if a date is today?

answered May 11, 2018 by rich-c-2789 (16,180 points)
edited May 11, 2018 by rich-c-2789
commented Apr 1, 2019 by lewis-s-9714 (1,020 points)
Has this changed? I'm using version 3.0.44, build 8148 and I'm trying to get a "true" condition from the following code but it's showing false for both:
qie.debug('isDateWithin a day past? ' + (qie.isDateWithin("4/1/2019", -1, 'DAY')));
qie.debug('isDateWithin a day? ' + (qie.isDateWithin("4/1/2019", 1, 'DAY')));
commented Apr 4, 2019 by rich-c-2789 (16,180 points)
edited Apr 4, 2019 by rich-c-2789
Should have stated that this applied to version 3.0.45 and newer.  There was a bug in prior versions where todays date was not inclusive, as intended, causing some confusion.
commented Apr 4, 2019 by lewis-s-9714 (1,020 points)
Oh, I see, we're on an older version that doesn't have the fix. Thank you for the clarification!
...