Get current date time from server and convert it into local time in c#

前端 未结 6 649
一生所求
一生所求 2021-02-04 14:51

Help: I have a server which is having time in GMT-07.00 hours. My local time is GMT+05.30 hours. I need to get current date and time from server and convert this date and time i

6条回答
  •  后悔当初
    2021-02-04 15:28

    So you have the server's time, and you know the server's time zone? Then you can get the local time like this:

    //Server: 09-Mar-2014 11:00:00 AM:
    var serverTime = new DateTime(2014, 3, 9, 11, 00, 00);
    
    var serverZone = TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time");
    var localZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
    
    var localTime = TimeZoneInfo.ConvertTime(serverTime, serverZone, localZone);
    // => 09-Mar-2014 11:30:00 PM
    

提交回复
热议问题