[英]Moment Timezone - Invalid Date
In the snippet below, parsing moment.tz('30 3 14:59', 'wd HH:mm', 'America/Los_Angeles');
在下面的代码段中,分析
moment.tz('30 3 14:59', 'wd HH:mm', 'America/Los_Angeles');
works fine but changing 14:59 to anything between 15:00 and 23:59 gives an Invalid date
. 可以正常工作,但是将14:59更改为15:00到23:59之间的任何值都将导致
Invalid date
。
var validTime = moment.tz('30 3 14:59', 'w d HH:mm', 'America/Los_Angeles');
console.log(validTime.format()); // 2018-07-25T14:59:00-07:00
var invalidTime = moment.tz('30 3 15:00', 'w d HH:mm', 'America/Los_Angeles');
console.log(invalidTime.format()); // Invalid date
https://jsfiddle.net/084e2k91/26/ https://jsfiddle.net/084e2k91/26/
What's the issue here? 这里有什么问题?
That's really interesting because I am also having the same problem, I think this is a bug in the moment.js
which is not converting the date from other time zones. 这真的很有趣,因为我也遇到同样的问题,我认为这是
moment.js
的错误,它没有转换其他时区的日期。 But when you try with do
then everything seems fine. 但是,当您尝试使用
do
一切似乎都很好。 So my suggestion would be let use do
for time being while time we raise this issue in GitHub. 所以我的建议是暂时让
do
,而我们在GitHub上提出这个问题。
And do
is the same as d
can be checked here in the docs . 并且
do
与d
相同,可以在docs中检查。 It means both return same day but one as a number
other as a string
. 这意味着两者都在同一天返回,但一个返回一个
number
另一个返回为string
。
With more experiment I found in IST after 18:30 the problem is starting. 在18:30之后,我在IST中进行了更多实验,问题开始了。 But the cause is really not understandable to me.
但是原因对我来说真的是无法理解的。 But maybe cause is the addition of timezone value to the real-time which is causing the error.
但是可能的原因是将时区值添加到实时中导致了错误。 Mean if I am in IST then
00:00 - 5:30
is the same time from when the error occurs. 意思是如果我在IST中,那么从发生错误开始的时间是
00:00 - 5:30
。
So updated code should be like following: 因此,更新后的代码应如下所示:
var myTime1 = moment.tz('30 3 14:59', 'wd HH:mm', 'America/Los_Angeles'); console.log(myTime1.format()); var myTime = moment.tz('30 3rd 14:59', 'w do HH:mm', 'America/Los_Angeles'); console.log(myTime.format()); //In IST the problem is starting from 18:30 var myTime1 = moment.tz('30 3 19:30', 'wd HH:mm', 'America/Los_Angeles'); console.log(myTime1.format()); var myTime1 = moment.tz('30 3rd 19:30', 'w do HH:mm', 'America/Los_Angeles'); console.log(myTime1.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data-2012-2022.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.