Apologies in advance for my beginner question.
Review the code snippet below:
var dt = new Date(t*1000);
var m = "0" + dt.getMinutes();
Depending on the t
variable (unix time), the output can be one of the following:
m = 054 // 54 minutes
m = 03 // 3 minutes
The formatting breaks when there are too many minutes. To solve this, we need to extract only the last two characters by using:
var m = "0" + dt.getMinutes();
m = m.substr(-2);
Resulting in:
m = 54 // 54 minutes
m = 3 // 03 minutes
Problem solved, right? However, repeating the variable assignment seems inefficient. I attempted:
var m = "0" + dt.getMinutes().substr(-2);
But encountered this error:
TypeError: dt.getMinutes(...).substr is not a function
Is there a more concise way to achieve this?