Skip to content

Instantly share code, notes, and snippets.

@matharchod
Created January 18, 2012 22:05
Show Gist options
  • Save matharchod/1636100 to your computer and use it in GitHub Desktop.
Save matharchod/1636100 to your computer and use it in GitHub Desktop.
A jQuery date script that appends the current date & time to a DOM element
function showDateIn(elem) {
var date = new Date();
var today = date.toLocaleDateString();
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm;
if (minutes < 10) {
minutes = "0" + minutes;
}
if (hours == 12) {
ampm = "PM";
} else if (hours > 11 && hours != 12) {
ampm = "PM";
hours = hours - 12;
}
else {
ampm = "AM";
}
var time = hours + ":" + minutes + " " + ampm;
$(elem).append(today + " &#64; " + time);
}
showDateIn('#div');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment