Getting current time in 12 hour format

faysal.ishtiaq_1

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

2,366 Views
About the Author

Discover me.......

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.