Convert Hours to 12 hour format...

Anyone have anything written already that will convert the 24hr format and return the hour, and am/pm? So 14 hours would give me 2 PM....? If not I'll start writing...

get the hours, and if the hours > 12 then hours = hours - 12

will that help?

cheers,

?:)

As Jayant said, although remember to set it to PM in that argument as well. (If > 12 then PM, etc.)

Here you go :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
holdClock = os.time();
        local theTime = display.newText("12:12:12:am", 225, 410, nil, 14)
        theTime:setTextColor( 255, 255, 255, 255 )
         
        local function clockTimer(event)
                myTime = os.time()
                
                _G.duration = _G.duration - 2.5
                
                --Stop audio if duration is reached
                if _G.duration == 0 then
                        audio.stop(1)
                end
                                
                --print(_G.duration)
        
                if holdTime ~= myTime then
                        holdTime = myTime;
                        t = os.date( "*t" )
                        ampm = "am";
 
                        if t.hour == 12 then
                ampm = "pm";
                        end
                        
                if t.hour < 12 then
                ampm = "am";
                        end    
                         
                if t.hour > 12 then
                t.hour = t.hour - 12;
                ampm = "pm";
                        end    
                         
                if t.hour == 0 then 
                t.hour = 12;
                ampm = "am"     
                        end
 
                        local s = string.format( "%2d:%02d:%02d%s", t.hour, t.min, t.sec, ampm )
                        --print( s );
                        theTime.text = s;
                end
        end
        
        Runtime:addEventListener( "enterFrame", clockTimer );
views:1382 update:2011/10/10 21:27:38
corona forums © 2003-2011