Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

two date fields, one can't be before another

Community Beginner ,
May 26, 2020 May 26, 2020

I need to make sure the End Date falls after the Start Date in my fillable pdf, can someone help with the script?

 

Start Date: MM DD YYY

End Date: MM DD YYY

 

I have attached the pdf, thank you!!

TOPICS
PDF forms
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
May 27, 2020 May 27, 2020

You can use this code as the custom calculation script of a (hidden) text field:

 

var startDateMonth = this.getField("part3.month").valueAsString;
var startDateDay = this.getField("part3.day1").valueAsString;
var startDateYear = this.getField("part3.year").valueAsString;
var endDateMonth = this.getField("part3b.month").valueAsString;
var endDateDay = this.getField("part3b.day1").valueAsString;
var endDateYear = this.getField("part3b.year").valueAsString;

if (startDateMonth && startDateDay && startDateYear && endDateMonth && endDateDay && endDateYear) {
	var startDate = util.scand("mmm dd yyyy", startDateMonth + " " + startDateDay + " "+ startDateYear);
	var endDate = util.scand("mmm dd yyyy", endDateMonth + " " + endDateDay + " "+ endDateYear);
	if (startDate && endDate && startDate.getTime()>=endDate.getTime()) {
		app.alert("Error! The end date must be after the start date.");
		this.resetForm(["part3b.month", "part3b.day1", "part3b.year"]);
	}
}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2020 May 26, 2020

This is a bit complicated to do with individual fields for month, day and year, as you would first need to validate that all the fields are filled-in, before merging them into a single string, then converting that string into a Date object and compared it with the Date object from the other field.

Can you combine them to a single field, you think?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 26, 2020 May 26, 2020

Unfortunately no, these fields have to be individual, can you help?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2020 May 27, 2020

You can use this code as the custom calculation script of a (hidden) text field:

 

var startDateMonth = this.getField("part3.month").valueAsString;
var startDateDay = this.getField("part3.day1").valueAsString;
var startDateYear = this.getField("part3.year").valueAsString;
var endDateMonth = this.getField("part3b.month").valueAsString;
var endDateDay = this.getField("part3b.day1").valueAsString;
var endDateYear = this.getField("part3b.year").valueAsString;

if (startDateMonth && startDateDay && startDateYear && endDateMonth && endDateDay && endDateYear) {
	var startDate = util.scand("mmm dd yyyy", startDateMonth + " " + startDateDay + " "+ startDateYear);
	var endDate = util.scand("mmm dd yyyy", endDateMonth + " " + endDateDay + " "+ endDateYear);
	if (startDate && endDate && startDate.getTime()>=endDate.getTime()) {
		app.alert("Error! The end date must be after the start date.");
		this.resetForm(["part3b.month", "part3b.day1", "part3b.year"]);
	}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 27, 2020 May 27, 2020

This is incredible! Thank you so much! It works!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2024 Mar 05, 2024

I have a similar issue but im my case I also need to be able to select the same day as well but for some reason its not working: 

if (event.value) {
var startDateString = this.getField("Date1_af_date").valueAsString;
if (startDateString=="") {
app.alert("Please complete the header date first.");
event.rc = false;
} else {
var startDate = util.scand("mm/dd/yyyy", startDateString);
var endDate = util.scand("mm/dd/yyyy", event.value);
if (endDate.getTime() >= startDate.getTime()) {
app.alert("The date of this inspection cannot be after completion of this form date.");
event.rc = false;
}
}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2024 Mar 05, 2024

If you want to allow same day, you need to check that end date is strictly larger (>) not (>=).

Since you use getTime() and since you probably won't select both fields within same second end date will always be larger, when you get dates set both dates to same time.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2024 Mar 05, 2024

Thank you Nesa for clarifying, it worked but I still get the alert I set up and have to click on it 3 more times before it works fine, is it because of the getTime?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2024 Mar 05, 2024

It's probably your script, try this:

if (event.value) {
 var startDateString = this.getField("Date1_af_date").valueAsString;
 if (startDateString=="") {
  app.alert("Please complete the header date first.");
  event.rc = false;} 
else {
 var startDate = util.scand("mm/dd/yyyy", startDateString);
 var endDate = util.scand("mm/dd/yyyy", event.value);
 startDate.setHours(0, 0, 0, 0);
 endDate.setHours(0, 0, 0, 0);
        
 if (endDate.getTime() > startDate.getTime()) {
  app.alert("The date of this inspection cannot be after the completion of this form date.");
  event.rc = false;}}}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 06, 2024 Mar 06, 2024
LATEST

Works like a charm, thank you very much Nesa.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2024 Mar 05, 2024

I have a similar issue but im my case I also need to be able to select the same day and as well but for some reason its not working: (so its previous dates and same day but not after the header date)

if (event.value) {
var startDateString = this.getField("Date1_af_date").valueAsString;
if (startDateString=="") {
app.alert("Please complete the header date first.");
event.rc = false;
} else {
var startDate = util.scand("mm/dd/yyyy", startDateString);
var endDate = util.scand("mm/dd/yyyy", event.value);
if (endDate.getTime() >= startDate.getTime()) {
app.alert("The date of this inspection cannot be after completion of this form date.");
event.rc = false;
}
}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines