Reputation: 83
I am trying to convert some dates and times to different timezones and Moment.js is not functioning properly for this. I have read through the documentation and I have seen numerous examples and it seems I am doing this correctly, but it is not properly converting Los Angeles time to New York time. What am I doing wrong?
JS
var timeSelected = 2020/12/04 17:00;
var clientTz = America/Los_Angeles
var presenterTz = America/New_York
var momentTz = moment.tz(timeSelected, clientTz).tz(presenterTz).format( 'YYYY/MM/DD HH:mm' );
Upvotes: 0
Views: 1939
Reputation: 8271
Check below. I have did some changes and added the proper libraries. SOmetime if you have not included moment-timezone-with-data.js this does not work for some timezones. I have changed the input format, if you want to change the format to your format from question you have to use
.tz(timeSelected, 'YYYY/MM/DD HH:mm', clientTz)
var timeSelected = '2020-12-04 17:00';
var clientTz = 'America/Los_Angeles'
var presenterTz = 'America/New_York'
var momentTz = moment.tz(timeSelected, clientTz).tz(presenterTz).format( 'YYYY/MM/DD HH:mm' );
console.log(momentTz)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone-with-data.js"></script>
Upvotes: 1
Reputation: 713
const timeSelected = '2020/12/04 17:00';
const clientTz = 'America/Los_Angeles';
const presenterTz = 'America/New_York';
const momentTz = moment
.tz(timeSelected, 'YYYY/MM/DD HH:mm', clientTz)
.tz(presenterTz)
.format('YYYY/MM/DD HH:mm');
There might be argument between time and timezone in moment.tz function. You must pass format of source string if it's not default.
Upvotes: 4