C#:确保DateTime.Now返回GMT + 1次

我正在使用DateTime.Now根据今天的日期显示某些内容,当在本地工作(马耳他,欧洲)时,时间显示正确(显然是因为时区)但当我上传到我的托管服务器(美国)时,当然DateTime.Now不代表正确的时区。

因此,在我的代码中, 如何将DateTime.Now转换为从GMT + 1时区正确返回时间

使用System.Core中的TimeZoneInfo类;

您必须为此将DateTimeKind设置为DateTimeKind.Utc。

 DateTime MyTime = new DateTime(1990, 12, 02, 19, 31, 30, DateTimeKind.Utc); DateTime MyTimeInWesternEurope = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(MyTime, "W. Europe Standard Time"); 

只有当你使用.Net 3.5时!

这取决于你所说的“GMT + 1时区”。 你的意思是永久UTC + 1,或者你的意思是UTC + 1还是UTC + 2,具体取决于夏令时?

如果您使用的是.NET 3.5,请使用TimeZoneInfo获取适当的时区,然后使用:

 // Store this statically somewhere TimeZoneInfo maltaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("..."); DateTime utc = DateTime.UtcNow; DateTime malta = TimeZoneInfo.ConvertTimeFromUtc(utc, maltaTimeZone ); 

您需要计算出马耳他时区的系统ID,但您可以通过在本地运行此代码轻松完成此操作:

 Console.WriteLine(TimeZoneInfo.Local.Id); 

从您的评论来看,这一点将无关紧要,但仅限于其他人……

如果您使用.NET 3.5,则需要自己计算夏令时。 说实话, 最简单的方法是成为一个简单的查找表。 计算出未来几年的DST变化,然后编写一个简单的方法,在特定的UTC时间返回偏移量,并对该列表进行硬编码。 您可能只想要一个带有已知更改的已排序List ,并在您的日期在最后一次更改之后交替1到2小时:

 // Be very careful when building this list, and make sure they're UTC times! private static readonly IEnumerable DstChanges = ...; static DateTime ConvertToLocalTime(DateTime utc) { int hours = 1; // Or 2, depending on the first entry in your list foreach (DateTime dstChange in DstChanges) { if (utc < dstChange) { return DateTime.SpecifyKind(utc.AddHours(hours), DateTimeKind.Local); } hours = 3 - hours; // Alternate between 1 and 2 } throw new ArgumentOutOfRangeException("I don't have enough DST data!"); } 

我不认为您可以在代码中设置一个属性,使DateTime.Now返回除代码执行的计算机的当前时间以外的任何内容。 如果你想要总是得到另一种时间,你可能需要包装另一个函数。 您可以通过UTC进行往返并添加所需的偏移量:

 private static DateTime GetMyTime() { return DateTime.UtcNow.AddHours(1); } 

(代码示例在Luke对DateTime.Now的内部工作方式发表评论后更新)