Skip to main content
Participant
September 19, 2012
Question

Convert string to time

  • September 19, 2012
  • 2 replies
  • 622 views

Anyone have a function to convert string to time? Or know where to find one?

07:18 convert to 7:18am

19:32 convert to 7:32pm

This topic has been closed for replies.

2 replies

Inspiring
September 19, 2012

Use Date Class.

For example:

trace(new Date(Date.parse("Wed Apr 12 19:18:17 GMT-0400 2000")).toLocaleTimeString());

shows 07:18:00 PM in EST time zone

While this

trace(new Date(Date.parse("Wed Apr 12 07:18:00 GMT-0400 2000")).toLocaleTimeString());

shows 07:18:00 AM in EST time zone.

To format per your requirements use:

trace(new Date(Date.parse("Wed Apr 12 19:18:00 GMT-0400 2006")).toLocaleTimeString().match(/^(\d{2,}\:\d{2,})|\w+$/g).join("").toLocaleLowerCase());

which returns

07:18pm

kglad
Community Expert
Community Expert
September 19, 2012

do you really want a time (actually date) object or a string. 

if a date object:

function dateF(s:String):Date{

    return new Date(null,null,null,s.split(":")[0],s.split(":")[1]);

}

if a string:

function dateF(s:String):String{

var n:int = int(s.split(":")[0]);

if(n>12){

return (n-12)+s.split(":")[1]+"pm";

} else {

return n+s.split(":")[1]+"am";

}

}