本文介绍了moment.tz给的结果不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据选定的时区转换日期.我为5分钟的时差看到相同的结果感到惊讶.例如,

var x = "2017-07-10T18:30:00.000Z"
var y = "2017-07-10T18:35:00.000Z"
var z = "2017-07-10T18:45:00.000Z"

并尝试将它们转换为使用moment.tz:

moment.tz(x, 'America/New_York').format('DD/MM/YYYY HH:MM:SS');
moment.tz(y, 'America/New_York').format('DD/MM/YYYY HH:MM:SS')
moment.tz(z, 'America/New_York').format('DD/MM/YYYY HH:MM:SS')

令我惊讶的是,所有3个结果均为"10/07/2017 14:07:00"是相同的.怎么了任何帮助将不胜感激.

解决方案

简短答案:

问题是您在 format() .请注意,对于SS(小数秒)和ss(秒),您也会遇到相同的问题.


关于您的代码示例的一般说明:

使用 moment.tz 进行解析使用给定时区(例如'America/New_York'), moment.tz 并非用于将输入转换为给定的时区.

您必须使用 tz() 将矩对象转换为给定时区的方法.

请注意,您输入的字符串以Z结尾,因此它以UTC表示时间.


正如马特·约翰逊(Matt Johnson)在评论中指出的那样,在您的情况下,即使moment.tz(input, zone)也会将输入转换为给定的区域,因为输入字符串包含Z(适用于UTC时区).无论如何,不​​鼓励使用这种方法.


这里有一个代码示例,其中解析UTC 时间字符串并将其转换为'America/New_York'时区:

 var x = "2017-07-10T18:30:00.000Z";
var y = "2017-07-10T18:35:00.000Z";
var z = "2017-07-10T18:45:00.000Z";

console.log( moment.utc(x).tz('America/New_York').format('DD/MM/YYYY HH:MM:SS') );
console.log( moment.utc(y).tz( 'America/New_York').format('DD/MM/YYYY HH:mm:ss') );
console.log( moment.utc(z).tz( 'America/New_York').format('DD/MM/YYYY HH:mm:ss') ); 
 <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.13/moment-timezone-with-data-2012-2022.min.js"></script> 

I was trying to convert dates according to selected timezone. I was surprised to see same result for dates with 5 mins of time difference.For ex,

var x = "2017-07-10T18:30:00.000Z"
var y = "2017-07-10T18:35:00.000Z"
var z = "2017-07-10T18:45:00.000Z"

and tried converting them to using moment.tz:

moment.tz(x, 'America/New_York').format('DD/MM/YYYY HH:MM:SS');
moment.tz(y, 'America/New_York').format('DD/MM/YYYY HH:MM:SS')
moment.tz(z, 'America/New_York').format('DD/MM/YYYY HH:MM:SS')

To my surprise, result was same for all 3 being "10/07/2017 14:07:00".What's going wrong? Any help will be appreciated.

解决方案

Short answer:

The issue is that you are using uppercase MM (month) instead of lowercase mm minutes in you format(). Note that, you have the same problem for SS (fractional seconds) and ss (seconds).


General note about you code sample:

Use moment.tz for parsing time string using a given timezone (e.g. 'America/New_York'), moment.tz is not made for converting input to given timezone.

You have to use tz() method to convert a moment object to a given timezone.

Note that your input string ends with Z so it represents time in UTC.


As Matt Johnson pointed out in comments, in your case even moment.tz(input, zone) would convert input to given zone because input string contains the Z (that stays for UTC timezone). Anyway this kind of approach is discouraged.


Here a code sample that parses UTC time string and converts it to 'America/New_York' timezone:

var x = "2017-07-10T18:30:00.000Z";
var y = "2017-07-10T18:35:00.000Z";
var z = "2017-07-10T18:45:00.000Z";

console.log( moment.utc(x).tz('America/New_York').format('DD/MM/YYYY HH:MM:SS') );
console.log( moment.utc(y).tz( 'America/New_York').format('DD/MM/YYYY HH:mm:ss') );
console.log( moment.utc(z).tz( 'America/New_York').format('DD/MM/YYYY HH:mm:ss') );
<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.13/moment-timezone-with-data-2012-2022.min.js"></script>

这篇关于moment.tz给的结果不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 15:15