Convert UTC/GMT time to local time

前端 未结 11 998
情深已故
情深已故 2020-11-22 04:39

We are developing a C# application for a web-service client. This will run on Windows XP PC\'s.

One of the fields returned by the web service is a DateTime field. Th

11条回答
  •  心在旅途
    2020-11-22 05:11

    DateTime objects have the Kind of Unspecified by default, which for the purposes of ToLocalTime is assumed to be UTC.

    To get the local time of an Unspecified DateTime object, you therefore just need to do this:

    convertedDate.ToLocalTime();
    

    The step of changing the Kind of the DateTime from Unspecified to UTC is unnecessary. Unspecified is assumed to be UTC for the purposes of ToLocalTime: http://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx

提交回复
热议问题