Moment.js 本地相对时间 [英] Moment.js local relative time

查看:185
本文介绍了Moment.js 本地相对时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中的日期设置为欧洲/伦敦时间.我正在使用 Moment.js 来显示相对时间,例如3 分钟前".这对我来说很好,因为我在同一时区,但例如,PST 时区的人会看到8 小时后".我该如何解决这个问题?

I have dates in my database set to Europe/London time. I am using Moment.js to show relative time e.g. "3 minutes ago". This works fine for me as I am in the same timezone, but for example, someone who is PST timezone would see "in 8 hours". How can I fix this?

我现在的代码是这样的:

My current code is like this:

$('time').text(moment('2016-01-22 18:00:00').fromNow());

推荐答案

为了回应 Jon 的回答,moment 的相对时间功能严格基于 UTC,因此您描述的行为实际上不会发生,除非您在解释原始时间戳当地时间.

To echo Jon's answer, moment's relative time functionality is strictly UTC based, so the behavior you describe won't actually happen, unless you are interpreting the original timestamp in local time.

很难说你是否这样做了,因为你没有给出输入字符串的样本值.

It's hard to say if you're doing that or not, as you didn't give a sample value of the input string.

如果您的时间确实基于 UTC,但未反映在输入字符串中,则使用 moment.utc 而不是 moment.

If your times are indeed UTC based, but that's not reflected in the input string, then use moment.utc instead of just moment.

不,伦敦与 UTC 不同.

And no, London is not the same as UTC.

这篇关于Moment.js 本地相对时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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