Getting current time in 12 hour format

faysal.ishtiaq_1 0 Tallied Votes 2K Views Share

This code snippet can be used to get the current time in 12 hour format using JS

var now = new Date();
var hh = now.getHours();
var min = now.getMinutes();
				
var ampm = (hh>=12)?'pm':'am';
hh = hh%12;
hh = hh?hh:12;
hh = hh<10?'0'+hh:hh;
min = min<10?'0'+min:min;
				
var time = hh+" : "+min+" "+ampm;
alert(time);