根据客户时区asp.net mvc显示时间 [英] display the time according the clien time zone asp.net mvc

查看:86
本文介绍了根据客户时区asp.net mvc显示时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器是美国,当我提交文章,博客等。它显示我的客户端网站的服务器日期时间。所以我需要根据用户的时区来确定日期时间值?



我尝试了很多但没有获得成功。请帮帮我

My server is USA and when i submit an article, blog etc . it show the server date time my client website. so i need date time value according to the user’s time zone?

i tried lot but not got desire success. Please help me

推荐答案

我用 TimeZoneInfo.ConvertTimeToUtc() [ ^ ]



请同样:

https://msdn.microsoft.com/en-us/library/system .datetime.touniversaltimetime(v = vs.110).aspx [ ^ ]

http://blog.gauffin.org/2012/11/22/displaying-datetime-using-the-user-timezone -in-asp-net-mvc / [ ^ ]

http://stackoverflow.com/questions/7577389/how-to-elegantly-deal-with-timezones [ ^ ]



-KR
I'd use TimeZoneInfo.ConvertTimeToUtc()[^]

Please this as well:
https://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime(v=vs.110).aspx[^]
http://blog.gauffin.org/2012/11/22/displaying-datetime-using-the-user-timezone-in-asp-net-mvc/[^]
http://stackoverflow.com/questions/7577389/how-to-elegantly-deal-with-timezones[^]

-KR


您可以根据所需的时区将日期时间存储到数据库中(无论服务器的时区不同)。



创建一个扩展方法,将服务器的时间转换为所需的时区,然后将其保存到数据库中。

在下面的示例中,我将服务器的时间转换为IST -



You can store the date time into database as per your required timezone (no matter server's timezone is different).

Create an extension method to convert server's time to required timezone and then save it into database.
In following example I am converting server's time to IST -

private static readonly List<timezoneinfo> TimeZoneInfoList = TimeZoneInfo.GetSystemTimeZones().ToList();</timezoneinfo>




public static DateTime ConvertedDateTime(this DateTime dateTime, bool convertToTimeZone = true)
       {
           if (!convertToTimeZone || string.IsNullOrEmpty("Indian Standard Time")) return DateTime.Now.ConvertedDateTime();

           var targetTimeZone = TimeZoneInfoList.SingleOrDefault(x => x.Id.Equals("Indian Standard Time", StringComparison.Ordinal));

           return targetTimeZone != null
               ? TimeZoneInfo.ConvertTime(DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified), TimeZoneInfo.Local, targetTimeZone)
               : DateTime.Now.ConvertedDateTime();
       }





在数据库中,您将转换日期时间,因此在显示时您只需要选择数据来自数据库并显示它而不进行任何转换。



注意:如果您有多国观众,此解决方案将不适用。



In the database you will have converted datetime, so at the time of display you just need to pick the data from database and display it without any conversion.

Note: This solution will not suitable if you have multi-national audience.


这篇关于根据客户时区asp.net mvc显示时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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