Monday, August 5, 2013

Javascript Change Date Time to Military Format

No leading 0 for hour, always leading zero for minute on the input time.

function COSconvertto24hourtime(otime,ampm){

var newtime = '';
if (typeof otime != 'undefined') {
var hh = otime.substring(0, otime.indexOf(':'));
var mm = otime.substring(otime.indexOf(':')+1, otime.indexOf(':') + 3);
if(hh == 12){
hh = 0;
}
if (ampm.indexOf('PM') >= 0) {
hh = 12 + parseFloat(hh);
}
if (hh < 10) {
hh = '0' + hh;
}
newtime = '' + hh + ':' + mm + ':00';
} else {
newtime = NULL;
}
return newtime;
}



No comments:

Post a Comment