使用带有from.ow()或from()的moment.js的时区 [英] Using timezones with moment.js fromNow() or from()

查看:141
本文介绍了使用带有from.ow()或from()的moment.js的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向用户显示自执行操作以来已经过了多长时间。

I want to show users how long has been elapsed since they performed an action.

行动发生的日期+时间存储在服务器上,服务器的时区。这就是造成麻烦的原因,因为如果用户的计算机的时区比服务器的时区提前12小时,那么如果用户现在添加了一些内容,则moment.js将显示'12小时前'作为的输出fromNow()而不是刚刚

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.

为了解决这个问题,我尝试以下方法:

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);

但尽管如此, timeAgo 仍然显示客户端的时区与服务器的时区之间的差异(即显示'12小时前'而不是'现在');

But despite of all this, timeAgo still shows the difference between the client's timezone and the server's timezone (i.e showing '12 hours ago' instead of 'now');

任何人都知道如何解决这个或我的问题做错了吗?

Anyone know how to fix this or what I'm doing wrong?

推荐答案

理想情况下,您希望将UTC时间戳从服务器传递到客户端。这并不意味着您必须将整个服务器切换到UTC,这只是意味着您可以在通过Web发送之前将数据库中的时间转换为服务器上的UTC。当然,如果您实际上以UTC格式存储时间会更好,但是您说您现在无法进行那种更改。但是,让我们假设您无法在服务器上进行任何更改。

Ideally, you would want to pass a UTC timestamp from your server to the client. 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. 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. But let's just work off the assumption that you can't change anything at all on the server.

我们还假设您的服务器已修复到UTC-07:00偏移量。在现实生活中,只有亚利桑那州不遵守夏令时的地方才会这样。因此,如果您在洛杉矶并且在太平洋时间,那么您的一些数据基于UTC-07:00,但其中一些数据基于UTC-08:00。如果你想用JavaScript做,那还需要做更多的工作。

We'll also assume that your server is fixed to the UTC-07:00 offset. 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. That requires a lot more work if you want to do it in JavaScript.

让我们假设输入已经是ISO8601格式的字符串。 (如果不是,请告诉我,我会调整此代码。)

Let's also assume that the input is already a string in ISO8601 format. (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.

这篇关于使用带有from.ow()或from()的moment.js的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆