Copy link to clipboard
Copied
I've got a String
variable for my time :
var $myTime:String;
//Some calculation
trace(myTime);
// Result is : 14:25 (for example)
I'm looking for a way to convert this string ("14:25
" in this example) to a simple AM / PM format.
So in this example it would be, for example, 2 PM
Any idea how I can simply do that ?
Another method:
var t:String = "13:20";
trace(amPm(t));
function amPm(time:String):String
{
var h:String = time.substr(0,2);
var m:String = time.substr(3,2);
var a:String = " AM";
if(parseInt(h) > 12){
a = " PM";
h = String(parseInt(h) - 12);
}
return h + ":" + m + a;
}
Copy link to clipboard
Copied
var myTime:String = "14:25";
function timeToAmPm(time:String):String{
var tmpArr:Array = time.split(":");
if(uint(tmpArr[0]) > 12){
tmpArr[2] = " PM";
}else{
tmpArr[2] = " AM";
}
return tmpArr.join("");
}
myTime = timeToAmPm(myTime);
Copy link to clipboard
Copied
Another method:
var t:String = "13:20";
trace(amPm(t));
function amPm(time:String):String
{
var h:String = time.substr(0,2);
var m:String = time.substr(3,2);
var a:String = " AM";
if(parseInt(h) > 12){
a = " PM";
h = String(parseInt(h) - 12);
}
return h + ":" + m + a;
}
Copy link to clipboard
Copied
That's quite a good there ! Thank you ! The only tiny thing :
If I've got :
$pmm= 05:23
$pms= 17:03
$bmm= 11:19
$bms= 23:40
amPm($pms) is returning 5 PM
and amPm($pmm) is returning 05 PM
Why pms doesn't have the 0 before the 5 ?