Print Page | Close Window

Automatically Setting the Due Date in an Invoice

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=2187
Printed Date: 17 Jun 2019 at 7:59pm
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: Automatically Setting the Due Date in an Invoice
Posted By: Andy63
Subject: Automatically Setting the Due Date in an Invoice
Date Posted: 27 Jun 2017 at 3:30am
OK Here is what I want.
1) I’m creating an invoice, with a text field (DateField) that will display the current date as soon as you open the form. I have customized the format as dd/mm/yyyy, and used the following document javascript to do this:
function date() {
 var fld = this.getField("DateField");
 fld.value = util.printd("dd mmmm yyyy",new Date());
}
date(); // call my function

2) What I want to do next is to add a second text field (DueDate), where the current date is advanced by 30 days, and this date is displayed in this text field in dd/mm/yyyy format, also as soon as you open the form.

I have tried a couple of JS options, none of which have seemed to work – no doubt in large part due to my ignorance of JS.

Is there a way to do this?



Replies:
Posted By: gkaiseril
Date Posted: 27 Jun 2017 at 8:59pm
Have you used Google to search for any JavaScripts to adjust the date object?

There are any number of tutorials about this. Some will show how to adjust the date using the number of milliseconds other tutorials will show how to use the getDate and setDate methods to adjust the date.

Both the following scripts will compute the date 30 days from now.

// using getDate/setDate;
var oDate = new Date();
console.println("The date is " + util.printd("mm/dd/yyyy", oDate));
// get the date of the month;
var nDate = oDate.getDate();
// add 30 days;
nDate += 30;
// set the date;
oDate.setDate(nDate);
console.println("In 30 days it will be " + util.printd("mm/dd/yyyy", oDate));

// using getTime and new Date;
var oDate = new Date();
console.println("The date is " + util.printd("mm/dd/yyyy", oDate));
// get the time in milliseconds since the Epoch date;
var nDate = oDate.getTime();
// milliseconds in one day;
var nOneDay = 24 * 60 * 60 * 1000;
// add 30 days in milliseconds;
nDate += 30 * nOneDay;
// create new date object;
var oNewDate = new Date(nDate);
console.pritnln("In 30 days it will be " + util.printd("mm/dd/yyyy", oNewDate));


Posted By: Andy63
Date Posted: 28 Jun 2017 at 7:39am
OK, this is puzzling...

The script works on the Javascript console, but no date displays in the text field.

Yeah, did try a few other pages and tutorials, but honestly got lost in it all.


Posted By: BAlheit
Date Posted: 28 Jun 2017 at 8:20am
You must set the value in the field.


Posted By: Merlin
Date Posted: 28 Jun 2017 at 3:28pm
You must absolutely read this article "The practicalPDF Date Library for Adobe Acrobat":
http://practicalpdf.com/the-practicalpdf-date-library-for-adobe-acrobat/ - http://practicalpdf.com/the-practicalpdf-date-library-for-adobe-acrobat/


-------------
ENG : https://www.abracadabrapdf.net/utilities-in-english/welcome/ - abracadabraPDF.net
FRA : https://www.abracadabrapdf.net - abracadabraPDF.net


Posted By: gkaiseril
Date Posted: 28 Jun 2017 at 4:32pm
Did you open the JavaScript console?


Posted By: Andy63
Date Posted: 29 Jun 2017 at 3:11am
Hello Again,
Firstly, apologies for being such an ignoramus with all this.

Hi Merlin
Yes I have tried reading a couple of articles online, but have generally come away more confused.

Hi gkaiseril
Yes I did open the javascript console, and the script itself works fine. What I can't get it to do is display the due date in the 'Due Date' text field.

Hi BAIheit
Sorry, I'm not sure what is mean by 'you must set the value in the field'.

Again, apologies for the rookie questions. Help is much appreciated.


Posted By: Andy63
Date Posted: 29 Jun 2017 at 3:12am
Just another question...

Should this be a document-level script, or a custom calculation script?

I have tried both approaches, but neither one seems to work.


Posted By: try67
Date Posted: 29 Jun 2017 at 11:08pm
If you want the code to run when the file is opened it needs to be placed as a doc-level field.
But first get it working... Run it from the Console and see what the results are.

By the way, if the field's format is "dd/mm/yyyy", why are you using a different format pattern string ("dd mmmm yyyy") in your code?


-------------
- Acrobat Expert - Contact me personally at try6767@gmail.com
Check out my custom-made scripts website: http://try67.blogspot.com


Posted By: Andy63
Date Posted: 30 Jun 2017 at 8:25am
Hi try67,

Yes, I have run it in the JS console - and it seems to have no problem setting the due date in that, but nothing displays in the 'Due Date' field (DueDate)

You're right about the format, I've adjusted it, but it has still made no difference.


Posted By: BAlheit
Date Posted: 30 Jun 2017 at 4:10pm
What is the name of the field?


Posted By: gkaiseril
Date Posted: 30 Jun 2017 at 4:56pm
You only need to add the additional code for computing the due date and filling in the due date field to your function.

function date() {
     var oSysDate = new Date(); // get the systems date object;
      var oDateField = this.getField("DateField"); // get today's date field;
      if(oDateField.defaultValue == "")
      {
           oDateField.value = util.printd("dd mmmm yyyy", oSysDate); // fill with formatted date;
//           oDateField.defaultValue = oDateField.value; // set default value;
          var oDue = new Date();
          oDue.setDate(oDue.getDate() + 30);
           var oDueDate = this.getField("DueDate"); // get due date field object;
           oDueDate.value = util.printd("dd mmm yyyy", oDue); // fill in due date;
//           oDueDate.defautlValue = oDueDate.value; // set value so it does not reset;
     } // end date field not set
} // end date function;

date(); // call my function


Posted By: gkaiseril
Date Posted: 30 Jun 2017 at 5:07pm
I would add the necessary code to compute the future date and set the due date field to your original function.

function date() {
     var oSysDate = new Date(); // get the systems date object;
      var oDateField = this.getField("DateField"); // get today's date field;
      if(oDateField.defaultValue == "")
      {
           oDateField.value = util.printd("dd mmmm yyyy", oSysDate); // fill with formatted date;
//           oDateField.defaultValue = oDateField.value; // set default value;
          var oDue = new Date();
          oDue.setDate(oDue.getDate() + 30);
           var oDueDate = this.getField("DueDate"); // get due date field object;
           oDueDate.value = util.printd("dd mmm yyyy", oDue); // fill in due date.
           oDueDate.defautlValue = oDueDate.value; // set value so it does not reset;
     } // end date field not set
} // end date function;

date(); // call my function;

You can also set the function to only update the date and due date if the default value of the date field is empty. This will prevent the field from being updated each time the form is entered and also stop the field from being reset if the form is reset.


Posted By: Andy63
Date Posted: 10 Jul 2017 at 3:41am
Hi Everyone,

Thanks for the help, finally found a script that works:
var oDate = new Date();
console.println("DateField" + util.printd("dd/mm/yyyy", oDate));
// get the date of the month;
var nDate = oDate.getDate();
// add 30 days;
nDate += 30;
// set the date;
oDate.setDate(nDate);
console.println("DueDate" + util.printd("dd/mm/yyyy", oDate));



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net