# 24 hour time conversion

**URL:** <https://boards.straightdope.com/t/24-hour-time-conversion/292059>\
**Category:** Factual Questions\
**Created:** [February 26, 2005, 10:53pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059 "2005-02-26T22:53:55Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![eagle](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@eagle](https://boards.straightdope.com/u/eagle)\
**Post date:** [February 28, 2005, 6:26am UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/21 "2005-02-28T06:26:13Z")

</div>

> [@Shalmanese](#):
>
> what about:
> 
> Hour12 = Hour24 % 12;  
> if ((Hour24 / 12) % 2)  
> AmORPm = “PM”;  
> else  
> AmORPm = “AM”;  
> day = Hour24 / 24;

Try 12:30 or 0:30 and you will get these results: 0:30 pm and 0:30 am. These are wrong and should read 12:30 pm and 12:30 am.

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [February 28, 2005, 12:12pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/22 "2005-02-28T12:12:14Z")

</div>

> [@Lumpy](#):
>
> Is there a conversion algorithm that, _without_ using an **IF Hour\>12** operation, you can plug the hours value into and always get a valid result?

Yes, there is. It is actually an interesting problem, in that it ends up being a friggin’ IQ test to write it. It took me 45 minutes. (In my defense, I did it in notepad instead of an IDE, which added development time.)

> [@eagle](#):
>
> And a remark to those thinking in programming languages: It is “IF (Hour24 \< 12) THEN AM else PM” and “if (Hour24 == 0) then Hour12=12 else if (Hour24 \> 12) then Hour12 = Hour24 - 12 else Hour12 = Hour24”. There is no easy way to do this with “mod”.

Mod returns the remainder, which is unhelpful. Here’s a big hint: you need an operator that performs integer division.

The challenge, as I read it:

Create a single line algorithm that returns a properly formatted 12-hour clock time string when passed an integer Hours and integer Minutes in 24-hour clock notation. No conditional statements are permitted, so no fair using IIF() immediate ifs.

I have done just such a thing, so rest assured it can be done. Try to figure it out yourself before verifying that my one liner works:

```auto

Public Function StandardTime(Hours As Integer, Minutes As Integer) As String
    StandardTime = 12 * (2 * Abs(Hours \ 13 - 1) - 1) + ((Hours - (Abs((Hours \ 13) - 1) * 12)) * Abs((Abs(Hours - 12) \ 12) - 1)) & ":" & Format(Minutes, "00") & " " & Chr(97 + 15 * (Hours \ 12)) & "m"
End Function

```

In case you can’t figure out what my algorithm is doing:

“” does regular division but only returns the integer part of the answer. (For example, 24 \ 12 = 2, whereas 23 \ 12 = 1, and 11 \ 12 = 0.)

“&” does string concatenation, automatically converting numbers (and anything else) into strings before concatenating.

Abs(AnyNumber) returns the absolute value.

Format(AnyNumber,“00”) pretties up the minutes to look like 01-09 and 11-59.

Chr(AnyNumber) returns the ascii character associated with that number. “a” is 97, for example.

---

<div class="post-metadata">

**Author:** ![spingears](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@spingears](https://boards.straightdope.com/u/spingears)\
**Post date:** [February 28, 2005, 12:59pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/23 "2005-02-28T12:59:32Z")

</div>

.  
Do nothing simply when a way can be found to make it complex and wonderful! :rolleyes:

😉

---

<div class="post-metadata">

**Author:** ![eagle](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@eagle](https://boards.straightdope.com/u/eagle)\
**Post date:** [February 28, 2005, 2:03pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/24 "2005-02-28T14:03:06Z")

</div>

Ellis, you use the wrong programming tool!

Java (untested):

import java.text.\*;

static DateFormat time12 = DateFormat.getTimeInstance(DateFormat.SHORT, [Locale.US](http://Locale.US));  
static DateFormat time24 = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.GERMAN);  
static {  
time24.setTimeZone(time12.getTimeZone());  
}

…  
String input24; // String with 0:00 - 23:59 time format.  
…  
**String output12 = time12.format(time24.parse(input24)); // this the one line that counts.**  
…

and vice versa:

…  
String input12; // String with 1:00 - 12:59 am/pm time format.  
…  
**String output24 = time24.format(time12.parse(input12)); // again the line that counts.**  
…

This took less than 5 minutes (including the time to lookup JavaDoc).

cu

---

<div class="post-metadata">

**Author:** ![Philster](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Philster](https://boards.straightdope.com/u/Philster)\
**Post date:** [February 28, 2005, 2:05pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/25 "2005-02-28T14:05:46Z")

</div>

I’m ashamed to be part of this thread. It’s is 09:18 in the East, and in six hours it will be 15:18. Ask me how I did that…go ahead…

I added 6 and 9…together.

Shortcut **that.**

---

<div class="post-metadata">

**Author:** ![Sevo](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Sevo](https://boards.straightdope.com/u/Sevo)\
**Post date:** [February 28, 2005, 3:49pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/26 "2005-02-28T15:49:56Z")

</div>

> [@pulykamell](#):
>
> Have our arithmetic skills really gotten so bad that we can’t subtract 12 anymore?

Apparently…yes. :smack:

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [March 1, 2005, 2:21am UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/27 "2005-03-01T02:21:30Z")

</div>

> [@eagle](#):
>
> Ellis, you use the wrong programming tool!
> 
> Java (untested):
> 
> import java.text.\*;
> 
> static DateFormat time12 = DateFormat.getTimeInstance(DateFormat.SHORT, [Locale.US](http://Locale.US));  
> static DateFormat time24 = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.GERMAN);  
> static {  
> time24.setTimeZone(time12.getTimeZone());  
> }

That is not an answer to the question I was addressing:

> [@Lumpy](#):
>
> Is there a conversion algorithm that, without using an IF Hour\>12 operation, you can plug the hours value into and always get a valid result?

…which is clearly asking for an algorithm. Your answer is a function call. While that internal function _may_ use an algorithm, as opposed to conditional commands, I would tend to doubt it. (Why bother, accept as an intellectual exercise? There is no point.)

I gotta say, I’m really not feeling the love here. Where’s the props for my mad coding skills? Ok, a fourth grader could handle the coding I posted. But the logic was impressive, at least a little, no?

Such a shameless plea for validation surely deserves a pitting…

(But c’mon, that was tough, and I was impressed with myself.)

---

<div class="post-metadata">

**Author:** ![Shalmanese](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Shalmanese](https://boards.straightdope.com/u/Shalmanese)\
**Post date:** [March 3, 2005, 8:21am UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/28 "2005-03-03T08:21:30Z")

</div>

> [@eagle](#):
>
> Try 12:30 or 0:30 and you will get these results: 0:30 pm and 0:30 am. These are wrong and should read 12:30 pm and 12:30 am.

Hour12 = ((Hour24 + 1) % 12) - 1;

---

<div class="post-metadata">

**Author:** ![Shalmanese](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Shalmanese](https://boards.straightdope.com/u/Shalmanese)\
**Post date:** [March 3, 2005, 8:23am UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/29 "2005-03-03T08:23:01Z")

</div>

Oops, that should be:

Hour12 = ((Hour 24 - 1) % 12 + 1);

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [March 3, 2005, 11:20am UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/30 "2005-03-03T11:20:54Z")

</div>

> [@Shalmanese](#):
>
> Oops, that should be:
> 
> Hour12 = ((Hour 24 - 1) % 12 + 1);

Does that return 12 if Hour24 is either 0 or 12? I’m guessing not. Unless (-1) % 12 + 1 = (11) % 12 + 1 = 12, that is.

My original algorithm, though functional, was unnecessarily complex. Optimally reduced, and removing the minutes and am/pm parts of it, the shortest possible algorithm appears to be:

Hour12 = Hour24 - 12 \* (Hour24 \ 13 + (Hour24 - 12) \ 12)

The problem is that you need to subtract 12 if the hours is \> 12, add 12 if the hours is 0, and leave hours alone for all other values.

Hour24 \ 13 will return 1 if hours \> 12, otherwise it will return 0. So you can multiply this “identity element” by 12 for a basic subtraction that will cover all values from 1-23. (For 1-12, subtract 12_0. For 13-23, subtract 12_1.) It needs some help with 0, though.

(Hour24 - 12) \ 12 will return -1 if hours = 0, otherwise it will return zero. So for values 1-23, this element is ignored. (Adding 0 has no effect.) For 0, however, it will return -1. Since the green term returns 0 if the hours is 0, this element converts the “identity element” to -1. Since subtracting -12 is the same as adding 12, this handles the unique situation of 0 properly.

---

<div class="post-metadata">

**Author:** ![Celyn](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@Celyn](https://boards.straightdope.com/u/Celyn)\
**Post date:** [March 3, 2005, 1:17pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/31 "2005-03-03T13:17:16Z")

</div>

O ye gods! Often enough I am amused because I usually write down time of day in the 24-hour clock, which seems to confuse a lot of USA people, but hey, thanks to the SDMB, I fear I might have completely lost the ability to understand it myself.

How will I EVER manage to get a train or a 'bus again?

I will get lost, and starve to death, and then guess whom I shall blame.

Grr, grumble, too damn clever for their own good, some people! :eek:

---

<div class="post-metadata">

**Author:** ![eagle](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@eagle](https://boards.straightdope.com/u/eagle)\
**Post date:** [March 3, 2005, 2:17pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/32 "2005-03-03T14:17:49Z")

</div>

> [@Ellis Dee](#):
>
> … the shortest possible algorithm appears to be:
> 
> Hour12 = Hour24 - 12 \* (Hour24 \ 13 + (Hour24 - 12) \ 12)

In post #18 I already posted the shortest term: Hour12 = 1+((Hour24+11) mod 12)

Also I already posted there part of the algorithm you tried to do in one equation using “div” as integer division and “mod” as modulo or remainder (equal because valid values are all positive).

All I said, was, that you cannot convert it easily with modulo. For our brains it is easier to understand rules like 0 -\> 12AM, 1-11 -\> 1-11AM, 12 -\> 12PM, 13-23 -\> subtract 12, get 1-11PM. You can even make this simpler: On \>=12 subtract twelve and make it PM; if 0 write 12 instead.

cu

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [March 3, 2005, 3:27pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/33 "2005-03-03T15:27:54Z")

</div>

> [@eagle](#):
>
> I already posted the shortest term

My bad.

slinks away in shame

---

<div class="post-metadata">

**Author:** ![Rayne\_Man](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@Rayne\_Man](https://boards.straightdope.com/u/Rayne_Man)\
**Post date:** [March 3, 2005, 4:54pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/34 "2005-03-03T16:54:47Z")

</div>

> [@Celyn](#):
>
> O ye gods! Often enough I am amused because I usually write down time of day in the 24-hour clock, which seems to confuse a lot of USA people, but hey, thanks to the SDMB, I fear I might have completely lost the ability to understand it myself.
> 
> How will I EVER manage to get a train or a 'bus again?
> 
> I will get lost, and starve to death, and then guess whom I shall blame.
> 
> Grr, grumble, too damn clever for their own good, some people! :eek:

The 24 hour clock just comes naturally to most Brits. My clock-radio , VCR and microwave are all in the 24 hour format and , as **Celyn** has said, all bus , plane and train timetables are also written this way. So we don’t have to think of a conversion , we just get on with it.

---

<div class="post-metadata">

**Author:** ![Airman\_Doors\_USAF](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Airman\_Doors\_USAF](https://boards.straightdope.com/u/Airman_Doors_USAF)\
**Post date:** [March 3, 2005, 5:07pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/35 "2005-03-03T17:07:50Z")

</div>

Now we’re into algoritms? Geez. What is this, the government? Can’t we leave it at something simple, or must it always be absurdly complicated to derive a simple answer?

---

<div class="post-metadata">

**Author:** ![Declan](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@Declan](https://boards.straightdope.com/u/Declan)\
**Post date:** [March 3, 2005, 6:03pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/36 "2005-03-03T18:03:46Z")

</div>

> [@Sevo](#):
>
> Apparently…yes. :smack:

Hey , thats what google is for

Declan

---

<div class="post-metadata">

**Author:** ![spingears](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@spingears](https://boards.straightdope.com/u/spingears)\
**Post date:** [March 4, 2005, 3:50pm UTC](https://boards.straightdope.com/t/24-hour-time-conversion/292059/37 "2005-03-04T15:50:07Z")

</div>

> [@Airman Doors](#):
>
> Now we’re into algoritms? Geez. What is this, the government? Can’t we leave it at something simple, or must it always be absurdly complicated to derive a simple answer?

You are absolutely correct!

Do nothing simply when a way can be found to make it complex and wonderful. :rolleyes:

[Previous page](https://boards.straightdope.com/t/24-hour-time-conversion/292059.md?page=1)
