如何在网页中将UTC时间显示为本地时间? [英] How to show a UTC time as local time in a webpage?

查看:207
本文介绍了如何在网页中将UTC时间显示为本地时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,该数据库保存时间为UTC.该时间可以显示在网页上,因此我被要求在页面中将其显示为本地时间,因为可以在任何国家/地区进行查看.一位同事提到了一些有关从当前线程(在服务器上)获取国家/地区设置的信息,但是我找不到任何详细信息.我想做的事可能吗?

I have a database that holds a time as UTC. This time can be shown in a webpage, so I’ve been asked to show it as local time in the page as it can be viewed from any country. A colleague mentioned something about getting the country settings from the current thread (on the server) but I couldn’t find any details on this. Is what I want to do possible?

推荐答案

如果您(和您的网站)对javascript感到满意,则有一种非常简单的方法来实现此目的.

If you (and your website) are comfortable with javascript, there is a very easy way to accomplish this.

首先,在服务器端,您应将UTC日期/时间格式化为RFC 3339格式(icalendar等协议使用的互联网时间标准). RFC 3339的基本语法为:

First, on the server side, you would have the UTC date/time formatted in RFC 3339 format (the standard for internet time used by, among other protocols, icalendar). The basic syntax of RFC 3339 is:

YYYY-MM-DDTHH:MM:SS

这样我在哪里,时间应该是:

Such that where I am, the time would be:

2010-05-04T05:52:33

但是,当时间不是本地时间而是UTC时,请在末尾添加Z来表示.因此,就我而言,由于我距格林尼治标准时间-0500小时,因此上述相同时间为:

But when the time is not local, but UTC, you add a Z to the end to denote this. So in my case, since I'm at -0500 hours from GMT, the same time above would be:

2010-05-04T10:52:33Z

因此,首先,您需要让服务器将上述内容输出到网页的javascript中.然后,javascript可以解析该时间戳,它将输出调整为浏览器时区(由托管浏览器的计算机确定)的日期和时间.您应该记住,如果用户来自东京,并且正在西班牙浏览您的网站,除非他们调整了计算机时钟,否则他们会看到东京时间的时间戳.

So, first you get the server to output the above to your web page's javascript. The javascript can then parse that timestamp and it will output the date and time adjusted to the browser's time zone (which is determined by the computer hosting the browser). You should remember that if a user is from Tokyo and is viewing your website in Spain, they will see the timestamp in Tokyo time unless they've adjusted their computer's clock.

因此javascript将是:

So the javascript would be:

    var time_string_utc = some_server_variable; // timestamp from server
    var time_string_utc_epoch = Date.parse(time_string_utc);
    var time_utc = new Date();
    time_utc.setTime(time_string_utc_epoch);

这时,您已将一个javascript日期对象设置为您的UTC时间戳.上面发生的事情的简要说明:

At this point, you have a javascript date object set to your UTC timestamp. A quick explanation of what happens above:

第一个变量假定您已将时间戳字符串从服务器传递给该变量.

The first variable assumes you have passed the timestamp string to that variable from the server.

第二个变量使用Date.parse()方法将字符串转换为纪元时间戳.

The second variable uses the Date.parse() method to convert the string to an epoch timestamp.

第三个变量创建未设置的Date对象.

The third variable creates the unset Date object.

最后一行使用setTime方法,该方法从纪元时间戳设置Date对象.

The last line line uses setTime method, which sets a Date object from an epoch timestamp.

现在有了对象,您可以根据需要将其输出给用户.作为一个简单的实验,您可以使用:

Now that you have the object, you can output it to the user as you see fit. As a simple experiment, you can use:

document.write(time_utc);

如果您使用的是UTC时间戳在我所在的时区,那么我会从以下位置开始:

which, if you are in my timezone using the UTC timestamp I started off with:

2010-05-04T10:52:33Z

将给出:

 Tue May 04 2010 05:52:33 GMT-0500 (CST)

但是您可以使用各种javascript方法将时间格式化为更令人愉悦的外观.

but you can use various javascript methods to format the time into something much more pleasant looking.

只要您相信用户所在的本地浏览器/计算机时区,就无需猜测用户所在的国家或什至无需调整时间戳.

No need to guess the user's country or even adjust your timestamp, so long as you trust the user's local browser/computer time zone.

又是简短的版本:

    var time_string_utc = some_server_variable; // UTC time from server
    var time_string_utc_epoch = Date.parse(some_server_variable);
    var time_utc = new Date();
    time_utc.setTime(time_string_utc_epoch);
    document.write(time_utc);

这篇关于如何在网页中将UTC时间显示为本地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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