Reputation: 129
I'm trying to set the timezone for a date in moment.js without changing the time value.
I get a date in utc:
const date = "2022-01-18T00:00:00.000Z";
And Im doing the next with moment-timezone
moment(date).tz("America/Bogota", true).format();
It is supposed to change the timezone without affecting the time but im getting:
2022-01-17T19:00:00-05:00
And what I expect:
2022-01-18T00:00:00-05:00
Upvotes: 0
Views: 1433
Reputation: 129
I fixed this way:
moment.utc(date).tz("America/Bogota", true).format();
Upvotes: 1
Reputation: 30675
I'd suggest perhaps using the moment.tz constructor with a format string. This way, we'll parse the input date as a local time.
const date = "2022-01-18T00:00:00.000Z";
console.log(moment.tz(date, "YYYY-MM-DD[T]HH:mm:sss.SSS", "America/Bogota").format());
.as-console-wrapper { max-height: 100% !important; }
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-1970-2030.min.js"></script>
Upvotes: 1
Reputation: 269
It seems as though .format
may be converting the date into local time.
The results I get with various time zones in moment
The date you provided converted from UTC to my local time
Upvotes: 0