Reputation: 679
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
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 ofmoment()
.
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