js 24 hour to 12 hour code example
Example 1: convert 24 hours to 12 hours javascript
function tConvert (time) {
time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];
if (time.length > 1) {
time = time.slice (1);
time[5] = +time[0] < 12 ? 'AM' : 'PM';
time[0] = +time[0] % 12 || 12;
}
return time.join ('');
}
tConvert ('18:00:00');
Example 2: how to conver time format to 12 hours in javascript
var hours = dt.getHours()
minute = dt.getMinutes();
hours = (hours % 12) || 12;
console.log("Time is - " + hours + ":" + minure;