DTul
DTul

Reputation: 679

moment.startOf month goes to 31st of previous month

In order to aggregate some things I need to set the month in a data Object. I'm trying to get the start of each month by using this code

moment("2018-09-04T13:06:07.397Z").startOf('month').toDate()

This however returns Date 2018-08-31T22:00:00.000Z.

How can I return the start of this actual month?

Upvotes: 2

Views: 1088

Answers (1)

VincenzoC
VincenzoC

Reputation: 31482

Your input string ends with Z, so represent time in UTC, you have to parse it using moment.utc(String) instead of moment(String)

By default, moment parses and displays in local time.

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().

console.log(moment.utc("2018-09-04T13:06:07.397Z").startOf('month').toDate());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Upvotes: 4

Related Questions