Skip to content

Instantly share code, notes, and snippets.

@ataylorme
Created November 17, 2012 23:21
Show Gist options
  • Save ataylorme/4101329 to your computer and use it in GitHub Desktop.
Save ataylorme/4101329 to your computer and use it in GitHub Desktop.
jQuery updating date time function
function updatingClock(selector, type) {
function currentDate() {
var currentDate = new Date;
var Day = currentDate.getDate();
if (Day < 10) {
Day = '0' + Day;
} //end if
var Month = currentDate.getMonth() + 1;
if (Month < 10) {
Month = '0' + Month;
} //end if
var Year = currentDate.getFullYear();
var fullDate = Month + '/' + Day + '/' + Year;
return fullDate;
} //end current date function
function currentTime() {
var currentTime = new Date;
var Minutes = currentTime.getMinutes();
if (Minutes < 10) {
Minutes = '0' + Minutes;
}
var Hour = currentTime.getHours();
if (Hour > 12) {
Hour -= 12;
} //end if
var Time = Hour + ':' + Minutes;
if (currentTime.getHours() Time += ' AM';
} //end if
if (currentTime.getHours() > 12) {
Time += ' PM';
} //end if
return Time;
} // end current time function
function updateOutput() {
var output;
if (type == 'time') {
output = currentTime();
if ($(selector).text() != output) {
$(selector).text(output);
} //end if
} //end if
if (type == 'date') {
output = currentDate();
if ($(selector).text() != output) {
$(selector).text(output);
} //end if
} //end if
if (type == 'both') {
output = currentDate() + ' at ' + currentTime();
if ($(selector).text() != output) {
$(selector).text(output);
} //end if
} //end if
setTimeout( updateOutput, 5000 ) //run update output every 5 seconds.
}//end update output function
updateOutput();
} // end updating clock function
updatingClock('#date-time', 'both');​
@ataylorme
Copy link
Author

jQuery function that outputs date/time and updates every minute. Fiddle is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment