admin管理员组文章数量:1423118
I have a string, 'Jan 24, 2019 12:00 PST', where the time is set to the browser's timezone. How would I use moment to convert this into another timezone (ie - America/New_York), and then get the time in that timezone?
So in my example above, I have the string 'Jan 24, 2019 12:00 PST' where the timezone is in the browser's timezone (Seattle). I am trying to get 'Jan 24, 2019 15:00' when converting it into New York timezone.
I have a string, 'Jan 24, 2019 12:00 PST', where the time is set to the browser's timezone. How would I use moment to convert this into another timezone (ie - America/New_York), and then get the time in that timezone?
So in my example above, I have the string 'Jan 24, 2019 12:00 PST' where the timezone is in the browser's timezone (Seattle). I am trying to get 'Jan 24, 2019 15:00' when converting it into New York timezone.
Share Improve this question asked Feb 27, 2019 at 1:05 JonJon 8,55131 gold badges96 silver badges150 bronze badges2 Answers
Reset to default 5How about just:
var s = "Jan 24, 2019 12:00 PST";
var d = new Date(s);
var m = moment(d).tz("America/New_York");
console.log(m.format("MMM DD Y, hh:mm z"));
The only worry here is where you got that string. That particular string works in every browser, as far as I know. But the Date constructor doesn't seem to support named offsets like "PST" outside the US, so if you got like CEST for central European time, that won't quite work. In that case, just chop off the PST and parse it with Moment's string parsing functions. Since you already know the string was generated in the same zone that the user's in, you don't need it anyway.
As an alternative to all of that, avoid needing that string entirely and either get the date in ISO from the source server, or if the string came from some other piece of Javascript, just get that Date object and pass it directly to Moment.
This ment highlights a problem with parsing timezones like "PST" moment.js - How to parse a date string string with a text timezone like 'PST'?
If you absolutely can't get the string in another format, you could do is split off the last three characters and do your own parison against an object like so:
var testDate = "Jan 24, 2019 12:00 PST";
var timezones = {
" GMT": " +0000",
" EDT": " -0400",
" EST": " -0500",
" CDT": " -0500",
" CST": " -0600",
" MDT": " -0600",
" MST": " -0700",
" PDT": " -0700",
" PST": " -0800"
};
const fixTimezone = dateStr => {
let zone = dateStr.slice(-4);
let time = dateStr.slice(0, -4);
return time + timezones[zone];
};
let fixedDate = fixTimezone(testDate);
console.log(fixedDate);
console.log(
moment(fixedDate)
.tz("America/New_York")
.format("MMM DD Y, hh:mm z")
);
Uses moment-timezone.
Should get Jan 24 2019, 03:00 EST
https://jsfiddle/ethantrawick/xectfsby/12/
本文标签: javascriptMoment JSHow to get time from another timezoneStack Overflow
版权声明:本文标题:javascript - Moment JS - How to get time from another timezone - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745389810a2656545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论