Nate Kahl
Posts: 0
Joined: Fri May 17, 2013 7:03 pm

How do you convert 24 hour time to am/pm using javascript?

My time input is automatically converted to 24 hour time format---when i call it and display it, I need to change it back to am/pm. Any suggestions?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How do you convert 24 hour time to am/pm using javascript?

Hello! If you have time in 24 hour time format let's say in variable "time24" and it's for example "15:45" you can use the following code:

codevar tmpArr = time24.split(':'), time12;
if(+tmpArr[0] == 12) {
time12 = tmpArr[0] + ':' + tmpArr[1] + ' pm';
} else {
if(+tmpArr[0] == 00) {
time12 = '12:' + tmpArr[1] + ' am';
} else {
if(+tmpArr[0] > 12) {
time12 = (+tmpArr[0]-12) + ':' + tmpArr[1] + ' pm';
} else {
time12 = (+tmpArr[0]) + ':' + tmpArr[1] + ' am';
}
}
}/code

Dan Hoeck
Posts: 0
Joined: Sat May 17, 2014 3:55 am

How do you convert 24 hour time to am/pm using javascript?

I tried this without luck. Should the code be triggered on the success of invoking the list service?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

How do you convert 24 hour time to am/pm using javascript?

Hi Dan,

Add this function as custom JS:prefunction convertTime24to12(time24){
var tmpArr = time24.split(':'), time12;
if(+tmpArr[0] == 12) {
time12 = tmpArr[0] + ':' + tmpArr[1] + ' pm';
} else {
if(+tmpArr[0] == 00) {
time12 = '12:' + tmpArr[1] + ' am';
} else {
if(+tmpArr[0] 12) {
time12 = (+tmpArr[0]-12) + ':' + tmpArr[1] + ' pm';
} else {
time12 = (+tmpArr[0]) + ':' + tmpArr[1] + ' am';
}
}
}
return time12;
}/preUse this code on time value mapping:prereturn convertTime24to12(value);/pre

Dan Hoeck
Posts: 0
Joined: Sat May 17, 2014 3:55 am

How do you convert 24 hour time to am/pm using javascript?

Thank you, this worked great!

Clansi Deena
Posts: 0
Joined: Wed Dec 03, 2014 10:12 am

How do you convert 24 hour time to am/pm using javascript?

You can get am the current time or passed date time to AM PM format through following function.

http://allinworld99.blogspot.in/2014/...

Code: Select all

   function getval() { 
         var currentTime = new Date() 
         var hours = currentTime.getHours() 
         var minutes = currentTime.getMinutes() 

         if (minutes < 10) 
             minutes = "0" + minutes; 

         var suffix = "AM"; 
         if (hours = 12) { 
             suffix = "PM"; 
             hours = hours - 12; 
         } 
         if (hours == 0) { 
             hours = 12; 
         } 
         var current_time = hours + ":" + minutes + " " + suffix; 
         show_message("Current Your System Time is : " + current_time); 
     }
Louis
Posts: 0
Joined: Wed Nov 05, 2014 2:03 pm

How do you convert 24 hour time to am/pm using javascript?

Just to add, if you are planning on showing dates/time throughout your app, it might be worth adding a library to handle it for you. In my app I use Moment.js.

http://momentjs.com/

buyi wen
Posts: 0
Joined: Mon Sep 07, 2015 2:03 pm

How do you convert 24 hour time to am/pm using javascript?

you can try this free online js beautifier to format the javsscript code.

Ayush Gautam Bafna
Posts: 0
Joined: Mon Feb 13, 2017 4:53 am

How do you convert 24 hour time to am/pm using javascript?

Thank you, this worked great!

Return to “Issues”