var date = \"2012-01-18T16:03\";
var date = new Date(date);
console.log(date.getMinutes());
console.log(date.getMinutes().length)
This returns 3.
I suggest:
var minutes = data.getMinutes();
minutes = minutes > 9 ? minutes : '0' + minutes;
it is one function call fewer. It is always good to think about performance. It is short as well;
For two digit minutes use:
new Date().toLocaleFormat("%M")
I assume you would need the value as string. You could use the code below. It will always return give you the two digit minutes as string.
var date = new Date(date);
var min = date.getMinutes();
if (min < 10) {
min = '0' + min;
} else {
min = min + '';
}
console.log(min);
Hope this helps.
I usually use this piece of code :
var start = new Date(timestamp),
startMinutes = start.getMinutes() < 10 ? '0' + start.getMinutes() : start.getMinutes();
It is quite similar to the @ogur accepted answer but does not concatenate an empty string in the case that 0 is not needed. Not sure it is better. Just an other way to do it !
Another short way is to fill the minutes with a leading zero using:
String(date.getMinutes()).padStart(2, "0");
Meaning: Make the string two chars long, if a char is missing then set 0
at this position.
See docs at str.padStart(targetLength, padString)
you should check if it is less than 10... not looking for the length of it , because this is a number and not a string