显示第一次从数据库读取ToUniversalTime()

时间:2012-08-27 10:13:35

标签: c# asp.net .net datetime timezone

我通过使用C#语言转换为数据库MYSQL中的 ToUniversalTime()来存储用户提交的帖子。

 BLLContext bll=new BLLContext();
 bll.DatePosted = DateTime.Now.ToUniversalTime();
 BLLContextRepo repo=new BLLContextReop();
 repo.SubmitContext(bll);

当我尝试使用 ToLocalTime()检索它时,它显示服务器的本地时间(它在美国)。但我想展示印度标准时间(IST)。 实际代码未显示印度标准时间

<asp:Label ID="Label1" runat="server" Text='<%# string.Format("{0: MMM d, yyyy "+"@"+" hh:mm tt}",((DateTime)DataBinder.Eval(Container.DataItem,"DateUpdated")).ToLocalTime()) %>' />

是否有像

这样的东西
<asp:Label ID="Label1" runat="server" Text='<%# string.Format("{0: MMM d, yyyy "+"@"+" hh:mm tt}",((DateTime)DataBinder.Eval(Container.DataItem,"DateUpdated")).ToLocalTime("Indian Standard Time")) %>' />

我怎样才能实现它?

1 个答案:

答案 0 :(得分:0)

您需要使用相关的DateTime转换TimeZoneInfo。例如:

TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Indian Standard Time");
DateTime indian = TimeZoneInfo.ConvertTimeFromUtc(originalDateTime, zone);

或者,您可能希望查看我正在编写的日期/时间API,Noda Time我想通过分离出“本地”的想法来使所有这些更清晰一些“你知道区域的日期/时间(没有时区),你知道偏离UTC的那个区域。

相关问题