Expand my Community achievements bar.

how do I display today's date and a date 1 month from today in livecycle designer

Avatar

Level 2

Hello,

Does anyone know how to display a future dates in 3 date fields that would be 1 month; 2 months and 3 months from today?

I am making a sales contract form and in a date field is today's date (which can be altered if the user needs to change it).  The javascript I have is:

if

(this.rawValue == null

){

var

msNow = (new

Date()).getTime();

var

d1 = new

Date(msNow);

this.rawValue

= util.printd("mmm/dd/yyyy",

d1);

}

else

{

this.rawValue

= rawValue;

}

Then in the next date field i need to display the date 1 month from today

The third date field 2 months from today

the fourth date filed 3 months from today.

Any suggestions would be greatly appreciated.

7 Replies

Avatar

Level 6

Hi,

I made example form for you. Date() returns current system date as the number of days since the epoch. You can add 30 days and it will be one month fron now.

BR,

Paul Butenko

Avatar

Level 10

Hi Peter,

If you need to be more exact than adding 30 days then you can use the following code;

function

addMonth(date)

{

  var year = date.getFullYear();

  var month = date.getMonth();

  var day = date.getDate();

  month

= (month % 12) + 1;

  year

+= parseInt(month / 12);

  var days = daysInMonth(month, year);

  if (day > days)

  {

    day

= days;

  }

  return new Date(year, month, day);

}

function

daysInMonth(iMonth, iYear)

{

  return 32 - new Date(iYear, iMonth, 32).getDate();

}

You need to pass in a Date object to addMonth so (assuming DateTimeField1 is your date field);

NextMonth.rawValue

= util.printd("mmm/dd/yyyy", addMonth(

= util.printd("mmm/dd/yyyy", addMonth(

Date.parse(DateTimeField1.formattedValue));

Bruce

Avatar

Level 1

I had a similar question a few weeks back and I found a post that I took code from that seemed to work for me. You can modify it however you like, I don't remember how the original code was, I think they had it set up to calculate exactly one year from the date entered.

I modified it to add an input number of months (numberMonths)  to the date entered when data is merged with the form (dateField1). This is my code. (I put it on validate event)

if (this.rawValue != null ){

// convert date string to a date time object

var oDate = util.scand("mm/dd/yyyy", page1.dateField1.formattedValue);

// extract the full Year

var sFullYear = oDate.getFullYear();

// extract zero based month

var sMonth = oDate.getMonth();

// extract date

var sDate = oDate.getDate();

// create formatted display string from month, date, and  year plus x amount of months

dateField2.formattedvalue = util.printd("mm/dd/yyyy", new Date(sFullYear, (sMonth + numberMonths.rawValue), sDate));

page1.dateField2.rawValue = dateField2.formattedvalue;

}

I hope this helps!

Avatar

Level 2

Hi Bruce,

Thanks, I do need it to be more exact and as I am a newbie to javascript I think I am having trouble assigning the correct "addmonth".  Maybe the synatx I put is incorrect or perhaps I have put the code into the wrong datetime field.  I have attached the file so you can see what I have done.  Could you please have a look??

thanks very much,

Peter

Avatar

Level 10

Hi Peter,

I've updated your form, I moved the code into a script object so it can be called from the calculate event of the three date fields.

I made the date fields "calculate - read only", the base date field is "calculate - user can override" so the user gets a warning message, you could remove this by making it "user entered - optional".

I changed the addMonths function so it takes a number of months, so it works 1, 2 or 3 months from the base date not one month following the previous calculated date. Which looking at your form I assume is what you would want, not one month following the previously calculated date.

There seemed to be a cut and paste error in my previous reply which would have caused your problems ... sorry.

Bruce

Avatar

Level 2

Hi Bruce,

Thanks very much for you last post on July 3rd. I notice that the attachment on the Forum is still marked as "Queued" and I was wondering if it would be possible for you to email it to me directly?

Thanks,

Peter

pmiles@cloverdalepaint.com

Avatar

Level 2

Bruce you are Brilliant!!

Thanks very much for all your help