Getting current time in 12 hour format


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

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

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