使用javascript来应用用户时区差异?

发布于 2024-12-06 19:26:57 字数 288 浏览 0 评论 0原文

我想过让我的用户必须在我的应用程序中选择他们自己的时区,但是用一些 JavaScript 让它自动执行怎么样?

一些伪代码:
日期在服务器端打印为

2011-09-28 11:00 UTC。当用户加载页面时,jquery 会遍历具有 utcDate 类的所有元素,并将 UTC 日期/时间替换为 UTC 与 UTC 之间的差异的日期。已应用用户本地时区。

这样做时我应该注意什么?这有什么明显的缺点吗?

谢谢

I thought about letting my users having to select their own timezone in my application, but what about instead making it automatic with some javascript?

Some psuedocode:
A date is printed serverside as <p class="utcDate">2011-09-28 11:00 UTC</p>. When the user loads the page, jquery walks through all elements with the utcDate class and replaces the UTC date/time with a date where the diff between UTC & the users local timezone has been applied.

What should I be aware of when doing this? Are there any obvious drawbacks to this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2024-12-13 19:26:57

如果您这样做(这没有任何问题),请注意,如果涉及很多元素,它可能会影响感知的页面加载时间。

请务必在 DOM 就绪事件处理程序中进行替换,而不是在页面加载时进行,因为这对用户来说非常明显。

或者,为什么不直接输出 js.snippet,通过 document.write 内联写入正确的日期,而不是输出 UTC 日期然后更改它? - 这将使页面加载更快。

如果你在页面上使用它的任何地方都是相同的日期/时间,你可以通过js生成一次,然后简单地输出存储在变量中的值。

If you do this (which there is nothing wrong with) be aware that it can affect perceived page-load time if there is a lot of elements involved.

Be sure to do the replacement in a DOM-ready event handler and not on page load, since this would be very visible to the users.

Alternativly instead of outputting the UTC date and then changing it, why not just output a js.snippet that will write the correct date inline via document.write? - This will be faster for pageload.

If it's the same date/time everywhere you use it on a page, you can just generate it once via js, and then simply output the value stored in a variable.

浅语花开 2024-12-13 19:26:57

我建议改为输出 Unix 时间戳。 js Date 对象 没有直接使用 UTC 字符串的功能(您' d 必须解析出每个组件),但它可以使用 setTime 函数消耗时间戳。

Date 对象自动本地化为用户的设置,并且可以直接转换为字符串(尽管这可能比您想要的更详细:“Wed Sep 28 2011 07:03:26 GMT-0400(东部夏令时间)”),所以你可能想自己格式化字符串:

$('p.utcDate').each(function () {
    var time = new Date();
    time.setTime(this.innerHTML);
    this.innerHTML = time.toDateString();
  });

作为一个额外的好处,toDateString 将处理 mdy 与 dmy 等的本地约定。

I'd recommend outputting the Unix timestamp instead. The js Date object doesn't have the capabilities to consume a UTC string directly (you'd have to parse out each component), but it can consume a timestamp using the setTime function.

The Date object automatically localizes to the user's settings and can be cast to a string directly (although that's probably more verbose than you'd like: "Wed Sep 28 2011 07:03:26 GMT-0400 (Eastern Daylight Time)"), so you'll likely want to format the string yourself:

$('p.utcDate').each(function () {
    var time = new Date();
    time.setTime(this.innerHTML);
    this.innerHTML = time.toDateString();
  });

As an added benefit, toDateString will handle local conventions around m-d-y vs d-m-y, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文