1110
1110

Reputation: 6829

How to convert string to date ignoring zone part?

Because of the way dates are stored I need to get them exact as stored but zone make me trouble.

moment("2020-10-28T08:41:00.000Z").format("YYYY-MM-DD HH:mm") // Result: 2020-10-28 09:41

But I need to get value of this string date without zone, so I cut of 000Z part:

moment("2020-10-28T08:41:00").format("YYYY-MM-DD HH:mm") // Result: 2020-10-28 08:41

And that is the real value I need.
What I am trying to get is some more elegant way to do this conversion from string to date without making function to cutOffZone()?

Upvotes: 1

Views: 53

Answers (2)

Terry Lennox
Terry Lennox

Reputation: 30685

If you use the moment.utc() constructor, it will use utc mode, so the time will not be converted to local time. From the moment documentation:

moment.utc(...) is utc mode. Ambiguous input is assumed to be UTC. Unambiguous input is adjusted to UTC.

// Use moment.utc() constructor...
const dt = new moment.utc("2020-10-28T08:41:00.000Z").format("YYYY-MM-DD HH:mm");
console.log(dt)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Upvotes: 2

Makaze
Makaze

Reputation: 1086

If you just need string manipulation:

"2020-10-28T08:41:00.000Z".split(".")[0]

Upvotes: 1

Related Questions