Max
Max

Reputation: 1844

moment js `format` method breaks time

I need to create current date with time (7 am) with UTC timezone, but when I'm trying to format it time changing to wrong value:

code:

var t_day = moment().utcOffset(0)
t_day.set({ hour: 7, minute: 0, second: 0, millisecond: 0 })
console.log(t_day.toISOString())
console.log(t_day.format('MMM DD, YYYY HH:MM'))

Output:

2020-10-28T07:00:00.000Z
Oct 28, 2020 07:10

From where this 10 minutes came and how to format date properly?

Upvotes: 1

Views: 178

Answers (2)

Majed Badawi
Majed Badawi

Reputation: 28434

mm would refer to minutes:

var t_day = moment().utcOffset(0)
t_day.set({ hour: 7, minute: 0, second: 0, millisecond: 0 })
console.log(t_day.toISOString())
console.log(t_day.format('MMM DD, YYYY HH:mm'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Upvotes: 1

M&#225;rio Garcia
M&#225;rio Garcia

Reputation: 575

MM in the format reffers to the month, what you're looking for is mm

console.log(t_day.format('MMM DD, YYYY HH:MM')) // MM reffers to the month

// the correct should be:
console.log(t_day.format('MMM DD, YYYY HH:mm'))

Upvotes: 0

Related Questions