getMinutes() > 9 problem

My weather app was displaying the time correctly only when getMinutes() was greater than 10, but if the clock displayed 1:02 pm my weather app would display 1:2 so, I fixed that issue with the code below.

$.getJSON(url, function(data) {
$("#location").html("Location: " + + ", " +;
var myDate = new Date();

			$("#localtime").html("Time: " + myDate.getHours() + ":" + '0' + myDate.getMinutes()).slice(-2);

However, my code created a different problem for me. Now if the clocked says that it’s 1:12pm, my weather app will display it as 1:012pm. Any suggestions on how to fix this issue?


To achieve what you are looking for you should check if minutes variable is lower than 10 before you decide to prepend 0 to it. I won’t give you a ready solution for this, but instead give you an example:

var dt = new Date();
console.log((dt.getMinutes() > 9 ? '' : '0') + dt.getMinutes()); // this will always display 2 digits

Thanks for the assistance.