Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  24] [ 1]  / answers: 1 / hits: 16315  / 10 Years ago, wed, february 12, 2014, 12:00:00
new Date(timestamp).getHours();
new Date(timestamp).getMinutes();


I have a timestamp, i convert it to h/m, ex.00:00



how can I convert it to the format with 2 digit style, what I have now is 0:0


More From » javascript

 Answers
160

You'll have to pad your digits.



Try this:



var h = new Date(timestamp).getHours();
var m = new Date(timestamp).getMinutes();

h = (h<10) ? '0' + h : h;
m = (m<10) ? '0' + m : m;

var output = h + ':' + m;


Or place the padding in a function:



function pad(val){
return (val<10) ? '0' + val : val;
}


And use that like this:



var h = new Date(timestamp).getHours();
var m = new Date(timestamp).getMinutes();
var output = pad(h) + ':' + pad(m);

[#72565] Tuesday, February 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
admin

Total Points: 468
Total Questions: 103
Total Answers: 103

Location: Equatorial Guinea
Member since Sun, Feb 14, 2021
3 Years ago
;