I have very simple problem, but couldn't find good simple DRY solution. I want to convert number of hours to HH:MM
format. My try with Moment.js is:
var hours = 10.5
var hour_string = moment(hours*3600*1000).format('HH:MM')
But unfortunately I get:
"11:01"
and have no idea why. Of course my wanted result is "10:30"
.
I'd like just do it in the easiest way, similar as I can do in Rails:
Time.at(hours*3600).utc.strftime("%H:%M")
Any ideas?
I have very simple problem, but couldn't find good simple DRY solution. I want to convert number of hours to HH:MM
format. My try with Moment.js is:
var hours = 10.5
var hour_string = moment(hours*3600*1000).format('HH:MM')
But unfortunately I get:
"11:01"
and have no idea why. Of course my wanted result is "10:30"
.
I'd like just do it in the easiest way, similar as I can do in Rails:
Time.at(hours*3600).utc.strftime("%H:%M")
Any ideas?
Share Improve this question edited May 21, 2019 at 18:11 Karol Selak asked May 19, 2017 at 11:21 Karol SelakKarol Selak 4,7748 gold badges40 silver badges68 bronze badges2 Answers
Reset to default 21Okay, I found the reason. "MM"
means months, not minutes, which are "mm"
. And the hour shift was caused by timezones, which we can omit using the utc
function. The final solution is:
moment.utc(hours*3600*1000).format('HH:mm')
let hour = 10;
let minute = 30;
moment({ hour, minute }).format('HH:mm');
// output: 10:30