I am currently working on Project 3.2 in the Web Server course. This project uses javascript to determine the date and time of the last update of the sensor readings. Unfortunately, the resulting time display contains only one digit for minutes and seconds when the value is less than 10. Could anyone suggest a “clean” way of adding a leading “0” to single digit minutes and seconds for this case?
Thanks,
Ed Wilson
3 Answers
Hi.
That needs to be fixed.
The updateDateTime() JavaScript function should be like this:
function updateDateTime() { var currentdate = new Date(); var datetime = currentdate.getDate() + "/" + ("00" + (currentdate.getMonth()+1)).slice(-2) + "/" + currentdate.getFullYear() + " at " + ("00" + currentdate.getHours()).slice(-2) + ":" + ("00" + currentdate.getMinutes()).slice(-2) + ":" + ("00" + currentdate.getSeconds()).slice(-2); document.getElementById("update-time").innerHTML = datetime; console.log(datetime); }
Regards
Sara
Sara,
You are just a “fountain” of knowledge…thank you so much! You may mark this question as resolved.
Ed Wilson