ASP.NET MVC控制器/视图中显示本地时间 [英] ASP.NET MVC Controller/View display local time

查看:782
本文介绍了ASP.NET MVC控制器/视图中显示本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储所有SQL的DateTime在UTC,这样的时间是一致的,无论客户端是打从站点时区一个ASP.NET MVC应用程序。

I have an ASP.NET MVC app that stores all SQL DateTime in UTC, so that the time is consistent regardless of time zone that the client is hitting the site from.

现在,我要重新显示正确的时间给用户,所以任何时候我在视图中显示一段时间我使用:

Now, I want to re-display the correct time to the user, so any time I display a time in my View I use:

timeVariable.ToLocalTime();

不过,.ToLocalTime()是基于服务器上,而不是客户端。

However, .ToLocalTime() is based on the server, not the client.

我需要处理这个JavaScript中的客户端?

Do I need to handle this in JavaScript on the client?

我大概可以打发时间区作为请求的一部分,并有控制器处理offest,但我假设有一个更好的方式来做到这一点。或者是不是有?

I probably could pass the time zone as part of the request and have the controller handle the offest, but I am assuming there is a better way to do this. Or isn't there?

在此先感谢您的帮助!

-Matt

推荐答案

几乎我所看到的翻译时间为当地时间所有站点要求用户创建帐户时指定他或她的时区。 Gmail是一个例子,但也有许多其他问题。

Almost all sites that I have seen that translate time to local time ask the user to specify his or her timezone when creating an account. Gmail is one example but there are many others.

我发现,许多解决方案试图使用JavaScript或反向查找的IP地址很容易失败和边缘情况。

I've found that many solutions trying to use javascript or reverse-lookup on the IP address are prone to failure and edge cases.

这篇关于ASP.NET MVC控制器/视图中显示本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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