Skip to main content
Participant
November 21, 2022
Question

Expiration Date Chart with Javascript

  • November 21, 2022
  • 1 reply
  • 753 views

Hello! I am creating an expiration date chart that has a date formatted field named “event_date”, and four other read only fields that will display a date in the future based on the event date: 7 Days, 30 Days, 12 Weeks, and 6 Months

 

I have scripted out 2 years’ worth of dates like this (an example for 7 days in the future):

 

var v = this.getField("event_date").valueAsString; 

if (v=="") event.value = ""; 

else { 

               if (v=="01/01/2023") event.value = "01/08/2023"; 

               else if (v=="01/02/2023") event.value = "01/09/2023"; 

               else if (v=="01/03/2023") event.value = "01/10/2023"; 

               else if (v=="01/04/2023") event.value = "01/11/2023"; 

 

This works, but as you can imagine, this is labor intensive, and will need to be manually updated every 2 years. Can this be done with javascript, so that the dates work, including accounting for leap years, and do not need to be updated periodically?

 

Note: For expiration dates that are in months which have fewer days than the event date month, could we force the expiration date to be the last date in the expiration month? Here’s a 6 month example:

 

Event Date          Expiration Date

8/28/23               2/28/2023

8/29/23               2/28/2023

8/30/23               2/28/2023

8/31/23               2/28/2023

 

Thank you!

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
November 21, 2022

Yes, this is possible. You will need to convert your strings to a Date object and then you'll be able to add full days to them. It will automatically include leap-days as well. The last part is also possible, but will require some additional coding to account for those situations. All in all it's not a trivial task, though.

You can start learning about Date objects in Acrobat JS here:

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3

 

I've developed similar scripts for my clients in the past, so if you're interested in hiring a professional to do the coding for you, feel free to contact me privately (click my user-name and then on Send a Message) to discuss it further.