Skip to main content
October 3, 2019
Answered

copy date to field with different formatting

  • October 3, 2019
  • 2 replies
  • 4880 views

Hi there

 

I am using the following sample code to copy a date from one field to other fields that all have different formatting. The sample codes works and updates the destination fields as expected but when I change it to copying the source field, I get nothing.

----- sample ----

// Get and Format Date
var currDate = new Date();
var strDate = util.printd("dd-mm-yyyy", currDate);
 
// Display date on pages
this.getField("CurrentDocDate").value = strDate;

 

The below code which I have tried as a calculated value (on blur and also on focus on the source and/or destination field) is not working as expected as the source date format is dd-mm-yyyy and the destination fields have the following date formatting: "dd/mmm/yyyy", "yyyy/mm/dddd", and "dd/mm/yy"

// Get and Format Date
var currDate = this.getField("BirthDate");
var strDate = util.printd("dd/mmm/yyyy", currDate);
 // Display date in Field
this.getField("BirthDate2").value = strDate;

 

Thanks for your guidance

Satnam

field values all I

    This topic has been closed for replies.
    Correct answer ls_rbls

    Hi , 

     

    what happens if you add this script  into BirthDate2 field properrties--->custom calculation script:

     

    var currDate = this.getField("BirthDate").value;
    var strDate = util.scand("dd-mm-yyyy", s);
    d.setDate(d.getDate()-0);
    event.value = util.printd("dd/mmm/yyyy", d);

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

    And in the same field properties, add this line  in the Format tab of field BirthDate2   --->Custom--->Custom Format Script:

    event.value=this.getField("BirthDate2")=util.printd ("dd/mmm/yyyy");

     

     

    You can also just try this line in the custom calculation script of BrthDate2 field:

    var s = this.getField("BirthDate").valueAsString;      <<<----------------------------NOTE THE USE OF valueAsString instead of .value
    if (s=="") event.value = "";
    else {
    var d = util.scand("dd-mm-yyyy", s);
    d.setDate(d.getDate()-0);
    event.value = util.printd("dd/mmm/yyyy", d);
    }

    2 replies

    Participant
    April 3, 2020

    Hi All -- I need help with this as well and I'm hoping you can assist.

     

    I am creating a PDF form

    one question is - Enter your Birthdate = 12/17/1970

    there is another field that should automatically tell me what day was 12/17/1970 --> Thursday

     

    so the formula should copy the birthdate (mm/dd/yyyy) but only return (dddd)

     

    Please help!   thanks

    Thom Parker
    Community Expert
    Community Expert
    April 3, 2020

    The answer is in the text above, you can find out more about dates here:

    https://www.pdfscripting.com/public/Date-and-Time-Handling.cfm

    https://www.acrobatusers.com/tutorials/date_time_part2/

     

    To solve this issue, convert the Birth Date to a date object with "util.scand()", then use "util.printd()" to extract the day of week name.

     

    Here's an example script: You will of cousre need to change the field names

     

    var cBDay = this.getField("Birthday").value;

    if(cBDay != "")

    {

       var dtBDay = util.scand("mm/dd/yyyy",cBDay);

       this.getField("WeekDay").value = util.printd("dddd",dtBDay);

    }

    Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
    Participant
    April 3, 2020

    Thank you Thom.

     

    This is Perfect!   at the beginning didn't work because I had the other field set as date as well.  I changed it to NONE and not it is working!

     

    you are the MASTER!!!!

     

    Thank you very much!

    ls_rbls
    Community Expert
    ls_rblsCommunity ExpertCorrect answer
    Community Expert
    October 4, 2019

    Hi , 

     

    what happens if you add this script  into BirthDate2 field properrties--->custom calculation script:

     

    var currDate = this.getField("BirthDate").value;
    var strDate = util.scand("dd-mm-yyyy", s);
    d.setDate(d.getDate()-0);
    event.value = util.printd("dd/mmm/yyyy", d);

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

    And in the same field properties, add this line  in the Format tab of field BirthDate2   --->Custom--->Custom Format Script:

    event.value=this.getField("BirthDate2")=util.printd ("dd/mmm/yyyy");

     

     

    You can also just try this line in the custom calculation script of BrthDate2 field:

    var s = this.getField("BirthDate").valueAsString;      <<<----------------------------NOTE THE USE OF valueAsString instead of .value
    if (s=="") event.value = "";
    else {
    var d = util.scand("dd-mm-yyyy", s);
    d.setDate(d.getDate()-0);
    event.value = util.printd("dd/mmm/yyyy", d);
    }

    October 4, 2019
    Thank you!
    October 4, 2019
    The second script was the solution that worked for me.