How to change time zone for an asp.net application

前端 未结 6 988
一个人的身影
一个人的身影 2020-12-31 03:43

I need to set default timezone for my ASP.NET to Asia/Dhaka or GMT+6 timezone. But i cannot find a way to change it globally. There is a lot of reference on Stackoverflow an

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 03:51

    There's very easy way to do it. Simply get the current UTC time and your timezone in two different variables. Then convert UTC to your timezone in third variable and use it anywhere. Here's how you do it.

    DateTime date1 = DateTime.UtcNow;
    
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Pakistan Standard Time");
    
    DateTime date2 = TimeZoneInfo.ConvertTime(date1, tz);
    

    Set your Time Zone in tz and then use "date2" anywhere.

提交回复
热议问题