C#: Making sure DateTime.Now returns a GMT + 1 time

后端 未结 3 2118
执笔经年
执笔经年 2020-12-28 09:22

I am using DateTime.Now to show something according to today\'s date, and when working locally (Malta, Europe) the times appear correctly (obviously because of

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-28 09:37

    Use the TimeZoneInfo class found in System.Core;

    You must set the DateTimeKind to DateTimeKind.Utc for this.

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

    Only if you're using .Net 3.5 though!

提交回复
热议问题