Expand my Community achievements bar.

Calculate Dates for a Week

Avatar

Former Community Member

Hi,

I'm working on a form where the user enters a date in a date field and I would like to write a javascript code to automatically fill in the dates for the rest of the week. So if a user enters 12/16/13 in StartDate field,  four other date fields automatically filll in with 12/17/13, 12/18/13, 12/19/13, 12/20/13 so the user doesnt have to do each one.

I would greatly appreciate any help.

Thanks,

vlad

3 Replies

Avatar

Level 10

Hi Vlad,

I think the tricky thing here would be how to reference the four other date fields.  If you can just call them Date1, Date2, Date3 and Date4 (and have them under the same subform as StartDate) then try this JavaScript in the change event of your StartDate field.

var currentDate = util.scand("d/mm/yy", xfa.event.newText);

if (currentDate !== null && xfa.event.newText === util.printd("d/mm/yy", currentDate))

{

    for (var i = 1; i <= 4; i++)

    {

        currentDate.setDate(currentDate.getDate()+1);

        this.resolveNode("Date"+i).rawValue = util.printd("yyyy-mm-dd", currentDate);

    }

}

You will have to change the "d/mm/yy" format to match whatever your display format is, "d/mm/yy" is my default format in Australia.  But hopefully this will give you one way to try.

The second line (the if statement) is if they are typing a date and not using the date picker and is checking they have typed a valid date.

Regards

Bruce

Avatar

Former Community Member

Hi! I have been trying to do the exact same thing but in the reverse.  I have a field called WeekEnding where the user inputs the week ending date (such as Sunday, 12/29/2013) and then I need the form to populate the fields for Monday, Tuesday, Wednesday... of the week prior (i.e. 12/23/13, 12/24/13, 12/25/13 ...)  The only catch for me is that I have the WeekEnding field set as a Date/Time Field outside of the table where the days of the week date fields are located (all named Monday, Tuesday, Wednesday, etc.).

Is there a simple way to apply the above mentioned code to what I am trying to do?

Thanks!

Ahnna

Avatar

Level 10

Hi,

here a sample for you.

Assuming you have a table row with 7 columns, where each is named "Day".

The first six columns are text fields and the last is the date field you select the date for sundays.

Put this script (FormCalc) into the date fields exit event.

if (not $.isNull) then

          var d = Date2Num($.formattedValue, "MM/DD/YYYY")

          if (Num2Date(d, "E") eq 1) then

                    for i = 0 upto 5 do

                              Day[i] = Num2Date(d - (6 - i), "MM/DD/YYYY")

                    endfor

          else

                    $host.messageBox("Selected Date is not a Sunday", "Wrong Day of the Week", 0, 0)

                    Day[*] = ""

          endif

endif

It will check if you have selected a date and if so if the day of the date is a sunday.

On sundays it will run a for loop to populate the first six fields (Monday to Saturday) with the dates of the previous week, otherwise it will show a warning and deletes all fields values.