如何在不使用 Javascript 的情况下在 ASP.Net 中呈现给定 UTC 日期时间值的本地时间?

发布于 2024-07-22 04:35:00 字数 55 浏览 2 评论 0原文

当您将值存储为 UTC 时,是否可以在不使用 Javascript 的情况下向用户显示当地时间?

Is it possible to display local times to users without using Javascript when you store the values as UTC?

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

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

发布评论

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

评论(3

调妓 2024-07-29 04:35:00

您需要服务器端了解客户端时区。 典型请求中没有足够的信息来做出该决定,您可以获得的最接近的是 Accept_Language 标头,它可能会给您提供线索,但几乎没有足够的用处(特别是如果客户端位于具有多个时区的国家/地区) 。

因此,您需要用户告诉您他们的时区,然后使用登录或 cookie 来存储该信息。

You would need serverside to be aware of the clients timezone. There isn't enough information in the typical request to make that determination, the closest you can get is the Accept_Language header which might give you a clue but is hardly useful enough (esp. if the client is in a country that has multiple timezones).

Hence you would need to user to tell you what their timezone is and then use a logon or cookie to store that info.

小草泠泠 2024-07-29 04:35:00

您可以在服务器端使用 vb.net、c# 或您使用的任何 .net 语言进行转换。 您将必须在某个地方转换为当地时间。

您提出了一个非常广泛的问题,没有细节,因此我无法推荐如何在服务器上执行此操作。

编辑

根据评论,我发现您遇到的问题是您想在没有javascript的情况下找出用户时区。 我总是让用户在注册时告诉我他们的时区。

一种并不完美的方法是使用地理 IP 查找服务,它会告诉您最有可能的用户位置,并为您提供比使用语言设置更好的粒度。

You could do the conversion on the serverside in vb.net, c# or whatever .net language your using. You are going to have to convert to the local time somewhere.

Your asking a very broad question with no detail so I can't recommend how to do this on the server.

Edit

Based on the comments I see the problem your having is that you want to figure out what the users timezone is without javascript. I always have the user tell me their timezone when they register.

One approach which won't be perfect would be would to be use a geo-ip lookup service that will tell you most likely where your user and give your better granularity then using the language settings.

眼中杀气 2024-07-29 04:35:00

认为这是你能做的最接近的事情:

using System.Globalization;

// get the first language from request (en, fr, ru)
var primaryLanguage = Request.UserLanguages.First().Split(";").First();
// find a culture by this language
var culture = new CultureInfo(primaryLanguage);
// if the culture is neutral, try to find the specific one
if (culture.IsNeutralCulture)
    culture = CultureInfo.GetCultures(CultureTypes.SpecificCultures).FirstOrDefault(o => o.TwoLetterISOLanguageName == primaryLanguage);
// get the string from a datetime
var datetimeText = culture ? DateTime.Now.ToString(culture) : DateTime.Now.ToString();

Think this is the closest you can do:

using System.Globalization;

// get the first language from request (en, fr, ru)
var primaryLanguage = Request.UserLanguages.First().Split(";").First();
// find a culture by this language
var culture = new CultureInfo(primaryLanguage);
// if the culture is neutral, try to find the specific one
if (culture.IsNeutralCulture)
    culture = CultureInfo.GetCultures(CultureTypes.SpecificCultures).FirstOrDefault(o => o.TwoLetterISOLanguageName == primaryLanguage);
// get the string from a datetime
var datetimeText = culture ? DateTime.Now.ToString(culture) : DateTime.Now.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文