Converting device time to 12-hour format

tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
edited February 2016 in Working with GS (Mac)

I'm sure this has been done before but I couldn't find a post that explained how to use a single expression to convert 24-hour device time (aka military time) to a 12-hour format without the use of a rule that checks to see if the hour is greater than 12.

I wanted something that would display the time 13:05:00 as 01:05:00 and the time 24:10:00 as 12:10:00.

Here's the expression for the hour value: padInt(mod(game.Clock.Hour,12)+((mod(game.Clock.Hour,12)==0)and(12)or(0)),2)

Explanation:
I started with mod(game.Clock.Hour,12) which returns a value from 1 to 11 except at 12 and 24 which both return 0. So I needed to figure out how to add 12 to that 0 for those two values. I realized that I could use conditional logic (and/or/==) to do this based on the result of the remainder when dividing by 12. Thus, the mod(,12) function in the second part of the expression, which returns the remainder. Note that padInt(,2) just forces a two-digit value so if you don't need that, you can ignore it.

The function results are therefore:
game.Clock.Hour • expression
1 • 01
2 • 02
3 • 03
...
10 • 10
11 • 11
12 • 12
13 • 01
14 • 02
15 • 03
...
22 • 10
23 • 11
24 • 12

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Comments

Sign In or Register to comment.