• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

convert my "String" to AM or PM hour in ActionScript 3

Contributor ,
Jan 21, 2018 Jan 21, 2018

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 ?

TOPICS
ActionScript

Views

553

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jan 22, 2018 Jan 22, 2018

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;

}

Votes

Translate

Translate
Advisor ,
Jan 22, 2018 Jan 22, 2018

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);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 22, 2018 Jan 22, 2018

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;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

LATEST

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines