Having problems with converting my DateTime to UTC

前端 未结 5 795
抹茶落季
抹茶落季 2020-12-23 13:25

I am storing all my dates in UTC format in my database. I ask the user for their timezone and I want to use their time zone plus what I am guessing is the server time to fig

5条回答
  •  再見小時候
    2020-12-23 13:56

    You need to set the Kind to Unspecified, like this:

    DateTime now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
    var utc = TimeZoneInfo.ConvertTimeToUtc(now , zone);
    

    DateTimeKind.Local means the in local time zone, and not any other time zone. That's why you were getting the error.

提交回复
热议问题