I wanna format the output after getting the values of Date.getHours()
, Date.getMinutes()
and Date.getSeconds()
.
Here is the long way:
var dt = new Date();
var HH = dt.getHours();
var mm = dt.getMinutes();
var ss = dt.getSeconds();
HH = HH < 10 ? '0' + HH : HH;
mm = mm < 10 ? '0' + mm : mm;
ss = ss < 10 ? '0' + ss : ss;
My question: How can I compress the last part to .toString('D2')
function?
What I wanna archive:
var dt = new Date();
var HH = dt.getHours().toString('D2');
var mm = dt.getMinutes().toString('D2');
var ss = dt.getSeconds().toString('D2');
p/s: .toString('D2')
is same meaning with Standard Numeric Format Strings. Like C# syntax:
int i = 1;
Console.WriteLine(i.ToString("D2")); // output: 01