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?
Catch up wih the Appery.io community on our forum. Here you'll find information on the lastest questions and issues Appery.io developers are discussing.
https://forum.appery.io/
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?
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
I tried this without luck. Should the code be triggered on the success of invoking the list service?
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
Thank you, this worked great!
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); 
     }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.
you can try this free online js beautifier to format the javsscript code.
Thank you, this worked great!
Working.