Skip to main content
ciaranmskea
Known Participant
November 12, 2019
Question

JavaScript that identifies if date entered is weekday or weekend?

  • November 12, 2019
  • 3 replies
  • 1402 views

Hi Guys

 

I'm currently using Adobe Acrobat X Pro. I'm trying to create a form so that when the user inserts a date into a field a JavaScript will identify if this is a weekday or weekend. Depending on which is entered it will then add a specific rate. i.e. Weekday = £65 and Weekend =£130.

 

Can someone please help me or point me in the right direction?

 

Thanks

This topic has been closed for replies.

3 replies

ciaranmskea
Known Participant
January 24, 2020

Thank you for your advice and video links.

 

I'm still having difficulty finding a script that will know if the date selected is a weekday or weekend and auto populate another field with a specific rate i.e. £65 Weekday,  £130 Weekend.

Thom Parker
Community Expert
Community Expert
January 27, 2020

Bernd provided the correct answer, but the first thing the script needs to do is to convert the text date entry into a Date Object. How this is done depends on the format. But lets just assume for this example that it's simple.

 

This script was designed to go into the Custom validate script of the date field.

 

var oMyDate = util.scand("mm/dd/yyyy", event.value);

if(oMyDate)

{

   var nDayOfWeek = oMyDate.getDay();

    if((nDayOfWeek == 0) || (nDayOfWeek == 6))

         this.getField("Rate").value = 130;

    else

         this.getField("Rate").value = 65;

}

else

    this.getField("Rate").value = 0;

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ls_rbls
Community Expert
Community Expert
November 13, 2019

Hi

 

++ Adding to suggestion what you are looking for in your script is found in this Thom Parker's tutorial:Working with date and time in Acrobat JavaScript 

 

https://acrobatusers.com/tutorials/date_time_part2 

Use the included PDF practice file for your script which does just what you're looking for:

 

https://acrobatusers.com/assets/collections/tutorials/legacy/tech_corners/javascript_corner/tips/2006/date_time_part2/DatesExample2_blank.pdf 

ls_rbls
Community Expert
Community Expert
November 13, 2019

I forgot to add, however, that you should start with Part 1 of the turorial found here:

 

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

Bernd Alheit
Community Expert
Community Expert
November 12, 2019

The getDay() method returns the day of the week (from 0 to 6) for the specified date.

Note: Sunday is 0, Monday is 1, and so on.