C# Dispay是从数据库读取ToUniversalTime()的时间
我正在使用C语言将用户提交的帖子转换为数据库MYSQL中的ToUniversalTime(),以存储该帖子C# Dispay是从数据库读取ToUniversalTime()的时间,c#,asp.net,.net,datetime,timezone,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()检索它时,它显示了服务器的本地时间(在美国)。
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")) %>' />
如何实现它?您需要使用相关的
TimeZoneInfo
转换DateTime
。例如:
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Indian Standard Time");
DateTime indian = TimeZoneInfo.ConvertTimeFromUtc(originalDateTime, zone);
或者,您可能想看看我正在编写的日期/时间API,我想通过将“本地”日期/时间(无时区)的概念与您知道时区的日期/时间区分开来,使所有这些都更清楚,你只知道UTC的偏移量。野田佳彦时间支持中等信任环境吗?@hungrycoder:不确定-我对该环境的各种要求了解得不够,无法回答。不过,它并没有做任何对安全性特别敏感的事情——它通常从自己的程序集读取资源,如果您愿意,它可以使用
TimeZoneInfo
。您至少可以试一试:)您提到的“zone.ConvertTimeFromUtc(originalDateTime,zone);”这两个都是区域,它抛出了错误成员“System.TimeZoneInfo.ConvertTimeFromUtc(System.DateTime,System.TimeZoneInfo)”不能通过实例引用访问;改为使用类型名称限定它