Page 1 of 2

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

Posted: Fri May 17, 2013 7:03 pm
by Nate Kahl
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?

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

Posted: Fri May 17, 2013 8:06 pm
by Maryna Brodina
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

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

Posted: Tue Jun 10, 2014 6:09 am
by Dan Hoeck
I tried this without luck. Should the code be triggered on the success of invoking the list service?

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

Posted: Tue Jun 10, 2014 7:45 am
by Kateryna Grynko
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

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

Posted: Wed Jun 11, 2014 4:07 pm
by Dan Hoeck
Thank you, this worked great!

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

Posted: Wed Dec 03, 2014 10:12 am
by Clansi Deena
You can get am the current time or passed date time to AM PM format through following function.

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

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

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

Posted: Wed Dec 03, 2014 10:25 am
by Louis
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/

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

Posted: Mon Sep 07, 2015 2:03 pm
by buyi wen
you can try this free online js beautifier to format the javsscript code.

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

Posted: Mon Feb 13, 2017 4:53 am
by Ayush Gautam Bafna
Thank you, this worked great!

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

Posted: Fri May 22, 2020 11:10 am
by Trupti Auti
Working.