Skip to content

Instantly share code, notes, and snippets.

@dark-swordsman
Created April 29, 2017 19:25
Show Gist options
  • Select an option

  • Save dark-swordsman/77e26ed111935efb9ce6616d65635c1e to your computer and use it in GitHub Desktop.

Select an option

Save dark-swordsman/77e26ed111935efb9ce6616d65635c1e to your computer and use it in GitHub Desktop.
Clock Function
var t = document.getElementById("currentTime");
function updateTime(){
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
// if hours is in the PM, make it non-military
if(hours > 12){
var d_hours = hours - 12;
}
// format the minutes
if(minutes < 10){
minutes = "0" + minutes;
}
// format the seconds
if(seconds < 10){
seconds = "0" + seconds;
}
// make the string
var t_str = d_hours + ":" + minutes + ":" + seconds;
// decide if AM or PM and update the string
if(hours > 11 && hours < 17){
t_str += " PM - Enjoy Your Afternoon!";
} else if(hours <= 11){
t_str += " AM - Enjoy Your Morning!";
}else if(hours >= 17){
t_str += " PM - Enjoy Your Evening!";
}
t.innerHTML = t_str;
}
updateTime(); // Called once so time updates as soon as the page loads
setInterval(updateTime, 1000); // set to update every 1 second after that
console.log("Clock Loaded!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment