Skip to main content
Participating Frequently
January 31, 2019
Answered

Javascript - Date Auto-Population Help

  • January 31, 2019
  • 1 reply
  • 708 views

A form that was previously working perfectly now seems to be giving me some grief with the New Year having come upon us. This form is set to start at the current month and go back one month concurrently as below:

1/2019   12/2018  11/2018  10/2018  9/2018

It was doing this until the new year of 2019 came up and now it's duplicating some dates and looks like..

1/2019  12/2018  12/2018  10/2018  10/2018

I'm looking for some help to fix this issue and get it back to the way it is meant to be updating.

Document level Javascripts are:

To get first field:

this.getField("Today").value = util.printd("mm/yyyy", new Date());   

To get other fields:

function getPastDate(Today, months){

    var v = this.getField(Today).valueAsString

    if (v=="") return "";

    var cDate = util.scand("dd/mm/yyyy",new Date("01/"+v));

    if (cDate== null){

        app.alert("Please enter a valid date of the form \"mm/yyyy\".")

        return "";

    } else {

        cDate.setMonth(cDate.getMonth() + months);

        return util.printd("mm/yyyy",cDate);

    }

}

Field level Javascript:

event.value = getPastDate("Today",-1);               results in 12/2018

event.value = getPastDate("Today",-2);               results in 12/2018

Please help!

Thank you!

This topic has been closed for replies.
Correct answer try67

I think the issue is with this part of the code:

new Date("01/"+v)

That is not how you instantiate a Date object. Run this code from the console and see what the output is:

new Date("01/02/2019")

You don't need to create a date object there at all... Just do it directly, like this:

var cDate = util.scand("dd/mm/yyyy","01/"+v);

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 1, 2019

I think the issue is with this part of the code:

new Date("01/"+v)

That is not how you instantiate a Date object. Run this code from the console and see what the output is:

new Date("01/02/2019")

You don't need to create a date object there at all... Just do it directly, like this:

var cDate = util.scand("dd/mm/yyyy","01/"+v);

ShaylaRAuthor
Participating Frequently
February 1, 2019

When the date changed to 2/1/2019 in the first field today, all the fields populated today. Will this coding fix this issue?

try67
Community Expert
Community Expert
February 1, 2019

It should do, yes.