copy date to field with different formatting

Guest
Oct 03, 2019 Oct 03, 2019

Copy link to clipboard

Copied

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

Views

1.5K

Likes

Translate

Translate

Report

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

correct answers 1 Correct answer

Adobe Community Professional , Oct 04, 2019 Oct 04, 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

...

Likes

Translate

Translate
Adobe Community Professional ,
Oct 04, 2019 Oct 04, 2019

Copy link to clipboard

Copied

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);
}

Likes

Translate

Translate

Report

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
Guest
Oct 04, 2019 Oct 04, 2019

Copy link to clipboard

Copied

Thank you!

Likes

Translate

Translate

Report

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
Guest
Oct 04, 2019 Oct 04, 2019

Copy link to clipboard

Copied

The second script was the solution that worked for me.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Oct 04, 2019 Oct 04, 2019

Copy link to clipboard

Copied

awesome! you're welcome

Likes

Translate

Translate

Report

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
Explorer ,
Jul 14, 2022 Jul 14, 2022

Copy link to clipboard

Copied

LATEST

The short version solved one of my problems.

Likes

Translate

Translate

Report

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 ,
Apr 03, 2020 Apr 03, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 03, 2020 Apr 03, 2020

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

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 ,
Apr 03, 2020 Apr 03, 2020

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 03, 2020 Apr 03, 2020

Copy link to clipboard

Copied

First, just putting this code verbatim into document script won't work. The code just shows how to do the calculation, it is not written for a specific use.  Really, it's written for the console, which is where I test code before putting it in a field.  

 

To make this work on a form (and this is what I would do) put this script into the Custom Validate script on the "Birthday" field, change the code to this:

 

if(event.value != "")
{
   var dtBDay = util.scand("mm/dd/yyyy",event.value);
   this.getField("WeekDay").value = util.printd("dddd",dtBDay);
}

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

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