从UTC日期时间转换与C#用户提供的时区 [英] Convert DateTime from UTC to a user provided time zone with C#

查看:147
本文介绍了从UTC日期时间转换与C#用户提供的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说,如果我有保存日期/时间在UTC时区数据库列,我能读懂他们到DateTime对象在用C#编写我的ASP.NET web应用程序。我该如何将它们转换为用户提供的时区?

Say, if I have a database column that holds date/time in UTC time zone, I can read them into DateTime object in my ASP.NET web app written with C#. How do I convert them to a user provided time zone?

推荐答案

这假定时间有一个 KIND = DateTimeKind.Utc

您可以使用 ConvertTimeFromUtc

You could use ConvertTimeFromUtc:

TimeZoneInfo.ConvertTimeFromUtc(time, userTimeZone);

TimeZoneInfo.ConvertTime

TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, userTimeZone);

您可能会想看看在 TimeZoneInfo.ConvertTime 该方法的来龙去脉。

You will probably want to check out the MSDN article on TimeZoneInfo.ConvertTime for the ins and outs of the method.

这可能是值得一读的所有关于MSDN 时区之间转换为好。它比你想象的要复杂得多。

It's probably worth reading all about converting between time zones on MSDN as well. It's more complex than you might think.

这篇关于从UTC日期时间转换与C#用户提供的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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