最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moment JS - How to get time from another timezone - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

How 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/

发布评论

评论列表(0)

  1. 暂无评论