Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Date within a date range

Avatar

Former Community Member
I've been trying for several hours trying to simply determine if a single date is within two other dates, but I can't seem to get it working.



I have two date fields on my form (startdate and enddate), and one textfield to display the result of a 1 (within) or 0 (not within). Here is my code.



var start = util.scand("yyyy-mm-dd", startdate.rawValue);

var end = util.scand("yyyy-mm-dd", enddate.rawValue);

var xx = 0;

if ( (Date.parse("2008/03/20") >= Date.parse(start)) &&

(Date.parse("2008/03/20") <= Date.parse(end) ) ) {

xx=1;

};

this.rawValue=xx;



Does anyone know what I am doing wrong?
3 Replies

Avatar

Level 6
var boolStartOK = (startdate.rawValue <= "2008-03-20");

var boolEndOK = (enddate.rawValue >= "2008-03-20");

if (boolStartOK && boolEndOK)

this.rawValue = "OK";

else

this.rawValue = "not OK";



http://www.jlangdon.ca

Avatar

Level 7
You have to keep track of what you are dealing with, i. e. object, function, method, property, string, number, etc.



"util.scand()" returns an object and not a value. You will have to use either the "Number()" constrictor or the "valueOf()" method to get the number of milliseconds from the epoch date.



var start = util.scand("yyyy-mm-dd", startdate.rawValue).valueOf();

var end = util.scand("yyyy-mm-dd", enddate.rawValue).valueOf();

var checkDate = util.scand("yyyy-mm-dd", "2008/03/20").valueOf();

var xx = 0;

if ( (checkDate >= start & checkDate <= end) {

xx=1;

};

this.rawValue=xx;



I prefer to use the date number rather than the date string since leading zeros and order of the date parts in a string affect the results of the comparison.

Avatar

Former Community Member
Thanks very much both of Jared and Geo, they both work great! I was ready to give up on this task. That's good to know about the number() and valueOf() function and your info Geo.



Scott