2

I have a date that I'm grabbing from Livecycle input form version and I want to perform some calculation. I want to add one year to that date but subtract a day from it. For example, if my date is 7/1/2018 then my future date should be 6/30/2019

I have function written that will add a year but I do not know how I'd subtract a day from it.

var currentDate = mydate.rawValue;

function AddYearToDate(currentDate)
{
    if (currentDate == null)
    {
        return null;
    }
    var futureDate = currentDate.toString();
    if (futureDate.length >= 10)
    {   
        var year = futureDate.slice(0, 4);
        return futureDate.replace(year, (parseInt(year)+1).toString());
    }
    else
    {
        return null;
    }
}
user2832411
  • 105
  • 1
  • 11
  • Your method of adding a year is flawed: the result of 2/29/2016 plus one year should not be 2/29/2017. You need to parse the string to a Date, then add a year and subtract a day. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) and [*How can I add 1 day to current date*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date) – RobG May 09 '18 at 11:24
  • Hi Rob. I've already tried one of your solutions. But it gives me invalid date error. var newDate = new Date(currentDate); xfa.host.messageBox(newDate.toString()); //invalid date var result = newDate.setDate(newDate.getDate() + 1); xfa.host.messageBox(result.toString()); //NaN return result; – user2832411 May 09 '18 at 13:32
  • Don't use the built-in parser, please read the links in the order suggested. – RobG May 10 '18 at 11:50
  • What type / format is `currentDate` supposed to be? – John Slegers May 10 '18 at 14:56

0 Answers0