javascriptpdffoxit

javascript time formatting issue when hour is "12"


I have a PDF form whereas it needs to be able to either be filled out either electronically, or with a good old fashioned pen. As such it includes time calculations with a "Time In" and a "Time Out". For simplification, the form includes the options to select "am" or "pm" accordingly. See form entry sample

So I think I have come up with a cleaver way that if used electronically, I can allow a user to input a time in "h:MM" format and to concat the "am" or "pm" selection putting the result into a hidden field with the "h:MM tt" format and performing my time calculation accordingly. The result in this new hidden field works perfectly for all times except "12:xx". When you choose "pm" it makes the concat result "0:00 am" and if you choose "am" it makes the result "12:00 pm". I cannot figure out how to combat that. Here is the code I am using to create the result in the hidden field:

//format for inputted time
var TimeFormat = "h:MM";
// field names
var str1Field = "TimeIn"
var str2Field = "TimeInTT"
// get field values
var str1 = this.getField(str1Field).value;
var str2 = this.getField(str2Field).value;
// concat strings if there is data
if(str1 != "" && str2 != ""){
var res = (TimeFormat, str1) + " " + str2;
event.value = res;
}

"TimeIn" is a time field "h:MM" and "TimeIn TT" is the "am" or "pm". Any help with this is greatly appreciated. Maybe there is a better way to do it? Thank you.


Solution

  • I found a solution to my issue. Just to recap, the problem was when I was attempting to concatenate a field formatted as "h:MM" with a radio button option either providing "am" or "pm" and outputting the result to a field formatted as "h:MM tt", the above JavaScript would work for all times except noon (12:00 pm) and midnight. (12:00 am). After hours of trying a combination of things, using

    event.value = res.replace("12:00", "0:00");
    

    gave me the result I wanted. Again, this was only an issue when calculating to a field formatted as "h:MM tt". If it was just a text field, there was no issue. However, I needed it to be a time field for additional time calculations. So the finished script looks like this after cleaning it up:

     // field names
     var str1Field = "Day1Pd1TimeIn" //Text Field
     var str2Field = "Day1Pd1TimeInPeriod" //Radio Button
     // get field values
     var str1 = this.getField(str1Field).value;
     var str2 = this.getField(str2Field).value;
     // concat strings if there is data
     if(str1 != "" && str2 != "Off")
     {
     var res = str1 + " " + str2;
     event.value = res.replace("12:00", "0:00");
     }
    

    Maybe this will help someone in the future.