简体   繁体   English

使用带有from.ow()或from()的moment.js的时区

[英]Using timezones with moment.js fromNow() or from()

I want to show users how long has been elapsed since they performed an action. 我想向用户显示自执行操作以来已经过了多长时间。

The date+time of the action happening is stored on the server, in the server's timezone. 发生操作的日期+时间存储在服务器的服务器时区中。 That's what's causing the trouble, since if the user's computer's timezone is 12 hours ahead of the server's timezone, then if the user adds something right now, moment.js will show '12 hours ago' as the output of fromNow() rather than just now . 这就是造成麻烦的原因,因为如果用户的计算机的时区比服务器的时区提前12小时,那么如果用户现在添加了一些内容,则moment.js将显示'12小时前'作为fromNow()的输出而just now

To try to solve this, I'm trying the following method: 为了解决这个问题,我正在尝试以下方法:

var actionTime = moment( action.timeStamp);//time of when user performed action 
var serverTime = moment().zone('-07:00'); //current server time

console.debug( serverTime);//outputs Wed Sep 11 2013 15:19:51 GMT-0700

var timeAgo = serverTime.from( actionTime);

But despite of all this, timeAgo still shows the difference between the client's timezone and the server's timezone (ie showing '12 hours ago' instead of 'now'); 但尽管如此, timeAgo仍然显示客户端的时区与服务器的时区之间的差异(即显示'12小时前'而不是'现在');

Anyone know how to fix this or what I'm doing wrong? 任何人都知道如何解决这个或我做错了什么?

Ideally, you would want to pass a UTC timestamp from your server to the client. 理想情况下,您希望将UTC时间戳从服务器传递到客户端。 That doesn't mean you have to switch your whole server over to UTC, it just means that you would convert from the time in your database to UTC on the server before sending it over the web. 这并不意味着您必须将整个服务器切换到UTC,这只是意味着您可以在通过Web发送之前将数据库中的时间转换为服务器上的UTC。 Sure, it would be even better if you actually stored times in UTC, but you said you aren't in a position to make that sort of change right now. 当然,如果你实际存储了UTC时间,那会更好,但是你说你现在无法进行那种改变。 But let's just work off the assumption that you can't change anything at all on the server. 但是,让我们假设您无法在服务器上进行任何更改。

We'll also assume that your server is fixed to the UTC-07:00 offset. 我们还假设您的服务器固定为UTC-07:00偏移量。 In real life, this would only be true for places like Arizona that don't follow daylight saving time. 在现实生活中,只有亚利桑那州不遵守夏令时的地方才会这样。 So if you are in Los Angeles and are in Pacific Time, then some of your data is based on UTC-07:00, but some of it is based on UTC-08:00. 因此,如果您在洛杉矶并且在太平洋时间,那么您的一些数据基于UTC-07:00,但其中一些数据基于UTC-08:00。 That requires a lot more work if you want to do it in JavaScript. 如果你想用JavaScript做这件事,那还需要做更多的工作。

Let's also assume that the input is already a string in ISO8601 format. 我们还假设输入已经是ISO8601格式的字符串。 (If it's not, then let me know and I will adjust this code.) (如果不是,请告诉我,我会调整此代码。)

var s = "2013-09-11 18:00:00";  // from action.timeStamp

var actionTime = moment(s + "-07:00", "YYYY-MM-DD HH:mm:ssZ");

var timeAgo = actionTime.fromNow();

The reason your other code didn't work is because in the first line, you are affected by the time zone of the browser. 您的其他代码不起作用的原因是因为在第一行中,您受浏览器时区的影响。 The zone setter in the second line just changes the zone for formatting, not changing the actual moment in time. 第二行中的区域设置器只更改区域以进行格式化,而不是更改实际时刻。

Also, when you dump a moment to the console for debugging, make sure you format it for output. 此外,当您将时刻转储到控制台进行调试时,请确保将其格式化为输出。 Otherwise you are just looking at its internal property values, which may or may not make sense directly. 否则,您只是查看其内部属性值,这可能会或可能没有直接意义。

I have solved it in a different way, maybe this option was not possible back when the question was asked, but might be easier now. 我以不同的方式解决了这个问题,也许这个选项在提出问题时无法回复,但现在可能更容易了。

I used moment-timezone.js (which requires moment.js 2.6.0+). 我使用了moment-timezone.js (需要moment.js 2.6.0+)。

I set de default timezone to my server's timezone like this: 我将默认时区设置为我的服务器时区,如下所示:

moment.tz.setDefault("America/New_York"); // "America/New_York" in my case

and then just use it normally. 然后正常使用它。 fromNow() will use the timezone in the client to calculate the time that has passed since that moment. fromNow()将使用客户端中的时区来计算自那时起已经过去的时间。

moment(myDatetime).fromNow();

i had the same issue and used the above comments to modify my code. 我有同样的问题,并使用上述注释来修改我的代码。 I did the following to get it resolved: 我做了以下事情来解决它:

  transform(value: string) {
    var time = moment(value).utc();
    return moment(time, "YYYYMMDD").fromNow();
  }

I was missing the .utc() to convert it before I applied the .fromNow() 在应用.fromNow()之前,我错过了.utc()来转换它

Things to note this is being used within a Pipe for Ionic 3 and the above code is from the pipe logic. 需要注意的是,这是在Pipe for Ionic 3中使用的,上面的代码来自管道逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM