Leap Year and "Dates: Adjust a Date" Action

Greg_703914Greg_703914 Posts: 14
edited March 13 in Questions

I am curious if I am not thinking about how to adjust a date correctly or there is a problem because the number of days in a year can vary due to leap year.

In my instance the calculated date was supposed to be 2019-05-01. The calculated date was one day off. The calculation is that it takes an existing date (which in this case was 5/1/2020) and is supposed to calculate 1 year previous but I only have the option of days so I chose 365.

How can I setup a formula to properly calculate a date that is 1 year previous that accounts for the leap year ( Which I am assuming is the issue here)? I am misunderstanding how it is calculating days in the Dates: Adjust a date action?

Tagged:

Comments

  • Jozef_783863Jozef_783863 Posts: 262 admin

    @Greg_703914 To clarify what happened, I added an example action setup below. The output was 2019-05-02, but the expected output in your case was 2019-05-01. Is that correct?

  • Greg_703914Greg_703914 Posts: 14

    That is correct.

  • Jozef_783863Jozef_783863 Posts: 262 admin

    @Greg_703914 I submitted a new Idea for offering more "Unit" options that I recommend upvoting.

    Dates, Date & Time Actions - Offer More "Units"
    https://community.pushbot.com/discussion/989

    Since this is not available today, I will work on obtaining a formula you can use to address your case. I will share the formula with you here shortly.

  • Jozef_783863Jozef_783863 Posts: 262 admin
    edited March 13

    @Greg_703914 Using Field: Field Formulas, you can reuse the Javascript printed below to get 2019-05-01 (instead of 2019-05-02). The only place where you need to make an edit is in fields['input-field']. This is where we reference the date field in all lowercase letters and dashes in place of spaces.

    var date = new Date(fields['input-field']);
    date.setFullYear(date.getFullYear() - 1);
    result = date.toLocaleDateString('en-US');
    // this will output in format `M/D/YYYY`, but the format can be changed using
    // the options described here: http://127.0.0.1:59955/Dash/dswnripx/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/DateTimeFormat.html
    
  • Greg_703914Greg_703914 Posts: 14
    edited March 16

    This worked quite well. In fact, I was able to remove a second step in my process that formatted the date after it was calculated. This solution allows me to both calculate and format the dates format in the same step. Thank you.
     image

Sign In or Register to comment.