These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Could someone Please help with 24 hr time

nala5755
Registered: Jan 20 2011
Posts: 5

this time will convert “8pm” or “8 pm” or “8:00pm” or “8:00 pm” or “8p” or “8 p” or “8:00p” or “8:00 p” or "8" or "815" to military time.
The problem I'm having is if you enter 1AM it will change it to 1:00 not 01:00. Could someone please help? I know it's messy and ugly code, but really pulling out my hair at this point
 
form1.Page1.ShiftStartTime::change - (JavaScript, client)
if (xfa.event.change.match(/[0-9\p\P\a\A\m\M\:]/) == null)
xfa.event.change = "";
-------------------------------------------------------------------
Display Pattern
time{HH:MM}
 
Edit Pattern
time{hh:MMA}|time{h:MMA}|time{hh:MM A}|time{h:MM A}|time{hh:MA}|time{h:MA}|time{hh:M A}|time{h:M A}|time{hh:MMA}|time{h:MMA}|time{hh:MM A}|time{h:MM A}|time{hh:MA}|time{h:MA}|time{hh:M A}|time{h:M A}|time{HH:MM}|time{H:MM}|time{H:M}|time{HH:M}|time{hh:MM}|time{h:MM}|time{hh:MM}|time{h:M}|time{h:M}
-------------------------------------------------------------------
form1.Page1.ShiftStartTime::exit - (JavaScript, client)
if (!(this.isNull)) {
var fldValue = this.formattedValue;
var dayIndicator
var str = fldValue;
//add the "m" if the user only enters p like 8p
if (str.search = "P" && str.indexOf("M") == -1 && str.indexOf("m") == -1){
dayIndicator = str + "m";
fldValue = dayIndicator
}
//add the "m" if the user only enters a like 8a
if (str.search = "A" && str.indexOf("M") == -1 && str.indexOf("m") == -1){
dayIndicator = str + "m";
fldValue = dayIndicator
}
//get the am/pm indicator and make it upper case
dayIndicator = fldValue.substr(fldValue.length - 2)
dayIndicator = dayIndicator.toUpperCase();
//this.formattedValue = fldValue
 
//get the Hours + Mins - set them where neccessary
var hrs
var mins
var regExpression = / /g
var temp = fldValue.substr(0, fldValue.length - 2)
if (temp.length <= 3){
hrs = temp;
hrs = hrs.replace(regExpression, "");
}
var lastColon = fldValue.indexOf(":");
if (lastColon == -1){
mins = ""
} else {
mins = fldValue.substr(lastColon + 1, 2)
hrs = fldValue.substr(0,lastColon)
}
 
//write the military time out also in 24 hour
//starting is 00:00 and not 24:00
//only when finishing is it 24:00
if ((dayIndicator == "PM")&&(hrs ==12)){
hrs = "00"
}
//starting is 00:00 and not 24:00
if ((dayIndicator == "AM")&&(hrs ==12)){
hrs = "00"
}
if (dayIndicator == "PM"){
hrs = parseInt(hrs) + 12
}
if (mins == ""){
mins = "00"
}
//For when users enter 8 it will add a zero to the front
//so the time box changes it to 08:00
if (hrs == "" && temp.length <= 2 && str.indexOf(":") == -1 && str.indexOf("A") == -1 && str.indexOf("a") == -1){
hrs = "0" + this.formattedValue;
hrs = hrs.replace(regExpression, "");
this.rawvalue = parseInt(hrs)
}
//For when users enter 815 it will add a zero to the front
//so the time box changes it to 08:15
if (this.formattedValue.length == 3 && str.indexOf(":") == -1 && str.indexOf("A") == -1 && str.indexOf("a") == -1 && str.indexOf("P") == -1 && str.indexOf("p") == -1){
hrs = "0" + this.formattedValue;
hrs = hrs.substr(0,2);
mins = str.substr(1,4);
}
this.formattedValue = hrs + ":" + mins
}

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You seem to be doing time formatting and parsing the hard way.

Read this series of articles: http://acrobatusers.com/tutorials/2006/date_time_part1Since the script is accepting a variety of time formats, it should use regular expressions to determine which format was entered and then use the corresponding time parsing.

For formatting use the "util.printf()" function.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script