Reputation: 12512
I have a time range expressed in military hours, e.g.
10-14
I need to make it look like this:
10:00 am - 2:00 pm
I came up with something like this, but I think it's a bit too convoluted. Can it be simplified even further?
var hrFr = "10",
hrTo = "14";
var t = (hrFr > 12 ? hrFr - 12 : hrFr) + ':00';
t += (hrFr >= 12 ? ' pm' : ' am') + ' - '
t += (hrTo == 24 ? ' midnignt' : (hrTo > 12 ? hrTo - 12 : hrTo) + ':00' + (hrTo >= 12 ? ' pm' : ' am'));
alert(t);
Upvotes: 0
Views: 60
Reputation: 559
assuming they are always whole numbers, may need to dress up a little for minutes
function adjustTime(hrTo)
{
return ((hrTo == 24 || hrTo == 0) ? ' midnight' : (hrTo > 12 ? hrTo - 12 : hrTo) + ':00' + (hrTo >= 12 ? ' pm' : ' am'));
}
I like this more because if the specs change, you don't have to reparse both to and from, just the function
Upvotes: 1
Reputation: 5557
You shoudn't duplicate the conversion:
function convHour( hr ) {
return (hr == 24 ? 'midnight' : (hr > 12 ? hr - 12 : hr) + ':00' + (hr >= 12 ? ' pm' : ' am')
}
var t = convHour( hrFr ) + ' - ' + convHour( hrto );
Upvotes: 0