How to convert a date to UTC properly and then convert it back?

后端 未结 5 2050
心在旅途
心在旅途 2021-02-13 19:48

I\'m struggling with converting DateTime to UTC, the concept and all, something I\'m not understanding correctly.

When I get a date time string, say \"7/10/2013\", I si

5条回答
  •  星月不相逢
    2021-02-13 20:22

    Do you want to store the date in local time? If you only care ablut the date portion, then you probably want to force the parsed time to Utc before saving it to the database:

    DateTime utcDate = DateTime.SpecifyKind(Convert.ToDateTime("7/10/2013"),DateTimeKind.Utc);
    

    That way when it's saved to the database it's already in UTC and will not include the time component.

提交回复
热议问题