Announcement

Collapse
No announcement yet.

Formatting a time field to show 12:00 am and 12:00 pm correctly

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Formatting a time field to show 12:00 am and 12:00 pm correctly

    I am trying to create a PDF form that calculates a time difference, however, I cannot get noon and midnight to display in a friendly fashion. Using h:MM tt format for my end users to better understand, but it assumes 12:00 is always PM. It does not allow them to put in 12:00 am. The only way to have 12:00 am is to input it as 0:00 am. This is going to be very confusing to my users as all other time entries can be successfully made in am/pm format. Any code to allow this to not only display correctly, but still also calculate? Thank you!

    Here is code for time calculation:

    //format for inputted time
    var TimeFormat = "h:MM";
    // field names
    var str1Field = "Day1Pd1TimeIn"
    var str2Field = "Day1Pd1TimeInPeriod"
    // 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;
    }


    This calculation code works, it is just the display of the input fields when distinguishing between 12:00 am and 12:00 pm are the issue.
    Last edited by intrepid505; 06-14-2018, 09:49 PM.

  • #2
    Dear intrepid505,

    I apologize for the inconvenience.As to this situation you mentioned,please help to email us the PDF form file sample with those JS scripts that you mentioned included for us to do further investigation.Please help to email it to support@foxitsoftware.com (Attn:Lisa) and also indicate this thread link. Thank you.

    Comment


    • #3
      Thank you, Lisa, I sent it.

      Comment


      • #4
        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.

        Comment

        Working...
        X