Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

rawValue of DateTimeField showing as "YYYY-MM-DD" Causing problems in javascript code

Avatar

Former Community Member
I have a DateTimeField on my form, regardless of what I put in Display Pattern, Edit Pattern, Data Pattern, or Validation Pattern, whenever I retrieve the rawValue in javascript code it shows as "YYYY-MM-DD".



One of the reasons this is a problem is that I need to see if the entry in this field is a Thursday. The way I would normally do this is something like:



var datStartDate = new Date(frmODTMainPage.txtDateAdRuns.rawValue);

if (datStartDate.getDay() == 4)

{

//It's a Thursday

}



The problem is that if Date() receives a string in the form "YYYY-MM-DD" it throws an "Invalid Date" error.



I could spend time parsing the rawValue and creating a new variable that has the date in the format javascript likes, but I think it is a bit wierd that a datefield would output the date in a format that is not compatible with how javascript expects dates to be formated.



So, is there a setting somewhere that I am missing that is causing this behavior?



Thanks.
5 Replies

Avatar

Former Community Member
This is completely off the top of my head, and I'm not sure what format JS expects for the Date object right now, but if you specify that format as the Display Pattern and then use:



var datStartDate = new Date(frmODTMainpage.txtDateAdRuns.formattedValue);



Does it work?



Chris

Adobe Enterprise Developer Support

Avatar

Former Community Member
Thank you very much, formattedValue worked fine.



Sorry I didn't mention it in my original post, but from trial and error, it seems that JS expects the date to be separated by slashes "/" not hyphens "-". At first I thought the problem had to do with the year being shown first (YYYY-MM-DD) but after some tests it seems that having the year first is acceptable as long as slashes are being used (YYYY/MM/DD).



Thanks again for the formattedValue tip. I'll use that method for my code. However, I would still appreciate it if someone out there knows if there is a setting to get rawValue on DateTime Fields to output "correctly" (?).

Avatar

Former Community Member
Hi John Hobart,

I think you can change the format of date-time field by setting.In the Object properties--->Field-->Display Pattern,this is a drop-down list,you can fill 'YYYY/MM/DD' in it ,then ok.

Avatar

Former Community Member
You can't format the rawValue. That's just the raw data in the background and it never gets "outputted" really. If need data in a specific format you need to set one of the formatting pictures and use a property other than rawValue. Other wise you'd need to take the rawValue into a script variable and then reformat it yourself for use in your script.



Chris

Adobe Enterprise Developer Support

Avatar

Former Community Member
I was having the same problem. After reading above comments, I wrote this script to convert the Acrobat date to JS format and then back again. I bet there is a more elegant way, but this works.



var execDate = form1.ExecDateField.rawValue;



//convert to JS format by replacing the "-" with "/"

execDate = execDate.replace(/-/g,"/");

var termDate = new Date(execDate);



** add your JS date operations here **



// convert back into acrobat format

var tempYear = termDate.getFullYear();

// add one to Month because JS uses "0" January, "1" for Feb, etc.

var tempMonth = termDate.getMonth() + 1;

var tempDay = termDate.getDate();

//put them in order using the "-" delimiter

termDate = tempYear + "-" + tempMonth + "-" + tempDay;



form1.termDateField.rawValue = termDate;



-Charles