Jonah Elbaz
Jonah Elbaz

Reputation: 325

MomentJS is sometimes adding the wrong amount of hours

So what i'm simply trying to achieve a feature where i add X.X amount of hours to a 24h time. The issue is for the time 00:00, an incorrect amount of hours is added

The code i wrote works for the most part. It works for every possible time except 0:00. If i have 01:30 and i add 1h it gives me 02:30. If I have 02:30 and i add 1.5h it gives me 04:00.

So heres this issue. When i have 00:00 and i add 1h i get 01:06...seems to make no sense and i was hoping someone with more momentJS experience might have some thoughts or ideas on the matter. Heres the code

If i have 00:00 and have 3.5 i get 03:06. This all only seems to happen when i start with 00:00. Kind of driving me crazy

console.log(timesheetRows[i][p]); //prints 0:00
console.log(Number(hours)); // prints 1
timesheetRows[i][p] = moment.utc(timesheetRows[i][p], 'hh:mm').add(Number(hours), 'hours').format('HH:MM');

Upvotes: 0

Views: 2667

Answers (1)

Titus
Titus

Reputation: 22484

You're using the wrong formats, hh stands for 12 hours and MM stands for two digits month number.

Use something like this:

moment.utc(timesheetRows[i][p], 'HH:mm').add(Number(hours), 'hours').format('HH:mm');

Upvotes: 4

Related Questions