Converting time format for display

Hi, 

Another newbie question I am hoping to find some answers. 

My db stores time in a TIME field (coming from MySQL on web to SQLite on device). These are by default stored as 23:59:99 format (ie 24H). I need to show these in the regular 12H mode ie 11:59 pm. 

I searched high and low and see references to strftime but I can’t seem to get it to work. Is there a sample anyone can point me towards?

Thanks in advance for all your help. 

Regards,

Kerem

Have you read this blog post?

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

Hi Rob, 

Thanks for taking the time to respond even on your weekend. Most appreciated. 

I did see the blog post but I thought the methods outlined were useful in dealing with time / date based calculations etc. All I need is to take a string that looks like 11:30:00 and spit it out like 11:30 am . Not sure if the methods outlined in the blog post will work for this. Am I totally missing the obvious? I apologize if this is the case. 

Thanks & best regards,

Kerem

Well the concepts are the same.  You have a string and you need to convert it to something else.  So start by parsing the time into its four components using the string.match().  At that point, you could just say:

[lua]

    local ampm = “am”

    if hour > 12 then

         ampm = “pm”

         hour = hour - 12

    end

[/lua]

But if you need to do any comparison then converting it to a timestamp is the best way.

Hi Rob,

No need to do any comparisons. This is just for display purposes. I know I can do all the string stuff. I was just thinking that I can’t be the first person to need this kind of stuff and so a library function might exist somewhere. If not I’ll write it and put it on Code Exchange then. I take it from your previous response that one does not exist as far as you know. Right? Thanks much for all your help. 

Regards,

Kerem

I quickly coded the following. Does the job. Probably not the most elegant way to go about it but will do for now. Thanks for your help.

local hourComponent = tonumber( string.sub (myTimeFieldInDB, 1, 2) ) local minuteComponent = tonumber( string.sub (myTimeFieldInDB, 4, 5) ) local convertedTime = "" local ampm = "" if hourComponent \< 12 then ampm = "AM" else ampm = "PM" if hourComponent \> 12 then hourComponent = hourComponent - 12 end end hourComponent = tostring(hourComponent) minuteComponent = tostring(minuteComponent) if minuteComponent == "0" then minuteComponent = "00" end convertedTime = hourComponent .. ":" .. tostring(minuteComponent) .. " " .. ampm print( "Converted time is " .. convertedTime)

Have you read this blog post?

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

Hi Rob, 

Thanks for taking the time to respond even on your weekend. Most appreciated. 

I did see the blog post but I thought the methods outlined were useful in dealing with time / date based calculations etc. All I need is to take a string that looks like 11:30:00 and spit it out like 11:30 am . Not sure if the methods outlined in the blog post will work for this. Am I totally missing the obvious? I apologize if this is the case. 

Thanks & best regards,

Kerem

Well the concepts are the same.  You have a string and you need to convert it to something else.  So start by parsing the time into its four components using the string.match().  At that point, you could just say:

[lua]

    local ampm = “am”

    if hour > 12 then

         ampm = “pm”

         hour = hour - 12

    end

[/lua]

But if you need to do any comparison then converting it to a timestamp is the best way.

Hi Rob,

No need to do any comparisons. This is just for display purposes. I know I can do all the string stuff. I was just thinking that I can’t be the first person to need this kind of stuff and so a library function might exist somewhere. If not I’ll write it and put it on Code Exchange then. I take it from your previous response that one does not exist as far as you know. Right? Thanks much for all your help. 

Regards,

Kerem

I quickly coded the following. Does the job. Probably not the most elegant way to go about it but will do for now. Thanks for your help.

local hourComponent = tonumber( string.sub (myTimeFieldInDB, 1, 2) ) local minuteComponent = tonumber( string.sub (myTimeFieldInDB, 4, 5) ) local convertedTime = "" local ampm = "" if hourComponent \< 12 then ampm = "AM" else ampm = "PM" if hourComponent \> 12 then hourComponent = hourComponent - 12 end end hourComponent = tostring(hourComponent) minuteComponent = tostring(minuteComponent) if minuteComponent == "0" then minuteComponent = "00" end convertedTime = hourComponent .. ":" .. tostring(minuteComponent) .. " " .. ampm print( "Converted time is " .. convertedTime)