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

后端 未结 5 1103
不知归路
不知归路 2021-02-13 19:59

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:14

    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.

提交回复
热议问题