Javascript change getHours to 2 digit [duplicate]

Solution 1:

Your's method is good. Also take a note of it

var date = new Date();
currentHours = date.getHours();
currentHours = ("0" + currentHours).slice(-2);

Solution 2:

You can do this using below code,

create function,

function addZeroBefore(n) {
  return (n < 10 ? '0' : '') + n;
}

and then use it as below,

c = addZeroBefore(deg);