Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Compare a date with system date

Avatar

Level 2

Hello,

I am using the below script (which I have found in this forum) in order to get a date from datefield1, add one year and give the date to datefield2.

It works fine.

var oDate = util.scand("yyyy-mm-dd", this.rawValue);
var sFullYear = oDate.getFullYear();
var sMonth = oDate.getMonth();
var sDate = oDate.getDate();
Datefield2.formattedvalue = util.printd("dd.mm.yy", new Date((sFullYear+1), sMonth, sDate));
Datefield2.rawValue = Datefield2.formattedvalue;

But now I would like to compare the new date (nDate [added one year]) with the current system date (cDate).

Does anyone have an idea how to compare dates? The result shall change the color as below.

if (nDate < cDate){
      Datefield2.fontColor="255,0,0";
}

Thanks a lot.

Regards,

ra_be

1 Accepted Solution

Avatar

Correct answer by
Level 4

Try using Javascript's Date Object:

var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();

if (myDate>today)
   {
   alert("Today is before 14th January 2010");
   }
else
   {
   alert("Today is after 14th January 2010");
   }

If that doesn't work, try parsing out the month, day, and year then do a comparison on each of those (remembering to convert month strings to an interger. E.g. if you are storing the date as: February 2, 2009,  February goes to 2).

Then your comparison would look like:

if(myYear == sysYear){

     if(myMonth == sysMonth){

          if(myDay == sysDay){

               //days are equal

          }

          else if(myDay < sysDay){

               //myDay is before sysDay
          }

          else{

               //myDay is after sysDay

          }

     }

}

basically follow that same logic for the month and year comparisons and that should work as well.  It would definitely be cleaner and much more easier using the Date Object though (http://www.w3schools.com/jsref/jsref_obj_date.asp)

View solution in original post

2 Replies

Avatar

Correct answer by
Level 4

Try using Javascript's Date Object:

var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();

if (myDate>today)
   {
   alert("Today is before 14th January 2010");
   }
else
   {
   alert("Today is after 14th January 2010");
   }

If that doesn't work, try parsing out the month, day, and year then do a comparison on each of those (remembering to convert month strings to an interger. E.g. if you are storing the date as: February 2, 2009,  February goes to 2).

Then your comparison would look like:

if(myYear == sysYear){

     if(myMonth == sysMonth){

          if(myDay == sysDay){

               //days are equal

          }

          else if(myDay < sysDay){

               //myDay is before sysDay
          }

          else{

               //myDay is after sysDay

          }

     }

}

basically follow that same logic for the month and year comparisons and that should work as well.  It would definitely be cleaner and much more easier using the Date Object though (http://www.w3schools.com/jsref/jsref_obj_date.asp)

Avatar

Level 2

First one is working. Thanks.

ra_be