Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding "0" if clock have one digit

i have some clock script. Everything is fine and it's work perfectly but... i have one problem. If at the clock is set one digit hour or minute like 1:5 clock not adding "0" digit before. This what i'v done but it does't work. Can u help me, much thx?

window.setInterval(function update_clock() {
    var currentTime = new Date();
    var currentHours = currentTime.getHours();
    var currentMinutes = currentTime.getMinutes();
    $.ajax({
        success: function (clock) {
            document.getElementById("hour").firstChild.nodeValue = currentHours;
            document.getElementById("minutes").firstChild.nodeValue = currentMinutes;
            if (currentMinutes.length == 1) {
                currentMinutes = "0" + currentMinutes;
            }
        }
    });
}, 999);
like image 558
Lukas Avatar asked Sep 05 '12 09:09

Lukas


People also ask

How to add 0 before number in JavaScript?

Use the String() object to convert the number to a string. Call the padStart() method to add zeros to the start of the string. The padStart method will return a new, padded with leading zeros string.

How make numbers to 2 digit in JavaScript?

Use the toFixed() method in JavaScript to format a number with two decimals. The toFixed() method formats a number with a specific number of digits to the right of the decimal.


3 Answers

You may use .slice to extract a portion of a string. Pass a negative number to it, in order to slice from the end of the string.

Therefore, the following is possible, and quite simple:

('0'+currentMinutes).slice(-2)

Concatenating with '0' makes sure that the target of the operation will always be a string. ('0'+currentMinutes) will yield a 2 or 3 letter string ("07" or "017", for instance). Slicing the last two characters off that string will give you a 0-padded two-digit number.

Note that the above would yield "00" if currentMinutes is 100, so it assumes that you know the values you'll be working with.

This could be extracted to something more reusable:

Number.prototype.zeroPad = function() {
   return ('0'+this).slice(-2);
};

That would allow you to write:

currentMinutes.zeroPad();

You could also make the length of the padding variable:

Number.prototype.zeroPad = function(length) {
   length = length || 2; // defaults to 2 if no parameter is passed
   return (new Array(length).join('0')+this).slice(length*-1);
};

Which could be called as:

currentMinutes.zeroPad(); // e.g. "07" or "17"
currentMinutes.zeroPad(3); // e.g. "007" or "017"

Note that while currentMinutes.zeroPad() will work, 7.zeroPad() would not.

like image 171
David Hedlund Avatar answered Oct 03 '22 23:10

David Hedlund


currentMinutes is a number, so it does not have the length property. Also, you must check the length before set the currentMinutes to the minutes element.

Something like:

var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();

$.ajax({
    success: function (clock) {
        if (currentMinutes.toString().length == 1) {
            currentMinutes = "0" + currentMinutes;
        }

        document.getElementById("hour").firstChild.nodeValue = currentHours;
        document.getElementById("minutes").firstChild.nodeValue = currentMinutes;
    }
});
like image 28
Marcelo De Zen Avatar answered Oct 03 '22 22:10

Marcelo De Zen


currentMinutes won't have a length property, as it's a Number, not a String.

You could force it to be a String.

if ((currentMinutes+'').length == 1) {
     currentMinutes = "0" + currentMinutes;
}

But, because you have a Number, you should make your condition...

if (currentMinutes < 10) {
     currentMinutes = "0" + currentMinutes;
}

If you were especially crazy, you could do...

var hoursMinutes = ((new Date)+"").match(/\d+:\d+(?=:)/)[0].split(":");
like image 9
alex Avatar answered Oct 03 '22 21:10

alex