本文介绍了MomentJS从UTC转换为所需的时区,而不仅仅是本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用momentjs,但是尝试将UTC时间转换为名称为'America/New_York'所指定的特定时区(不一定是当前用户本地时区)时遇到问题. 此SO问题相似,但并没有真正帮助.

I am using momentjs but having an issue trying to convert a UTC time to a specific timezone (not necessarily local to the current user) that is specified by name 'America/New_York'. This SO question is similar but didn't really help.

我的思维过程是使用从服务器接收到的日期创建一个utc矩obj,然后将该UTC时间格式化为特定时区以进行显示.我目前正在如何处理的一小段代码:

My thought process is to create a utc moment obj with the received date from the server and then format that UTC time to the specific timezone for display purposes. A small snippet of how I'm currently approaching this:

var cutoffString = '20170421 16:30:00'; // in utc
var utcCutoff = moment.tz(cutoffString, 'YYYYMMDD HH:mm:ss', '+00:00');
var displayCutoff = 
        moment.tz(utcCutoff.format('YYYYMMDD HH:mm:ss'), 'YYYYMMDD HH:mm:ss', 'America/New_York');

console.log('utcCutoff:', utcCutoff.format('YYYYMMDD hh:mm:ssa Z')); // => utcCutoff: 20170421 04:30:00pm +00:00
console.log('displayCutoff:', displayCutoff.format('YYYYMMDD hh:mm:ssa Z')); // => displayCutoff: 20170421 04:30:00pm +00:00

我在这里的假设是,displayCutoff将是在美国/纽约时间"中显示的utcCutoff时间.但是当前显示的时间与utcCutoff对象相同.我还应该提到,使用.utc()而不是.tz并在应用.local()之后尝试操纵时区也不起作用.

My assumption here is that displayCutoff would be the utcCutoff time displayed in 'America/New_York' time. But it currently is displays the same time as the utcCutoff object. I also should mention that using .utc() instead of .tz and trying to manipulate the timezone after applying .local() did not work either.

任何帮助/指导将不胜感激.

Any help/guidance would be appreciated.

推荐答案

您可以使用> > ,因为您输入的是UTC字符串.您可以使用 tz 来转换您的时刻反对给定的时区.

You can use moment.utc since your input is an UTC string. You can use tz to convert your moment object to a given timezone.

请注意, tz 使用 moment.tz 解析函数,用于使用给定区域构建新的矩对象.当您这样做时:

Please note that the tz function converts moment object to a given zone, while you are using moment.tz parsing function that builds a new moment object with the given zone. When you do:

var displayCutoff = 
    moment.tz(utcCutoff.format('YYYYMMDD HH:mm:ss'), 'YYYYMMDD HH:mm:ss', 'America/New_York');

您不是将utcCutoff转换为'America/New_York',而是在纽约为20170421 16:30:00建立新的矩对象.

you are not converting utcCutoff to 'America/New_York' but you are building a new moment object for 20170421 16:30:00 in New York.

这是您代码的更新版本:

Here an updated version of your code:

var cutoffString = '20170421 16:30:00'; // in utc
var utcCutoff = moment.utc(cutoffString, 'YYYYMMDD HH:mm:ss');
var displayCutoff = utcCutoff.clone().tz('America/New_York');

console.log('utcCutoff:', utcCutoff.format('YYYYMMDD hh:mm:ssa Z')); // => utcCutoff: 20170421 04:30:00pm +00:00
console.log('displayCutoff:', displayCutoff.format('YYYYMMDD hh:mm:ssa Z')); // => displayCutoff: 20170421 12:30:00pm -04:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data-2010-2020.min.js"></script>

这篇关于MomentJS从UTC转换为所需的时区,而不仅仅是本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 22:42