Open utterances-bot opened 1 year ago
Love the short and simple explanation! Thank you Davide, you are awesome!!!
Oh, wow, thanks ❤ You're too kind!
One of the issue I had was when reading the DateTime
back from the database is that it is unspecified. So if the policy is to have the datetime as UTC, you have to specify it's kind when reading it back DateTime.SpecifyKind(v, DateTimeKind.Utc)
.
I use a value converter with EF core
var utcConverter = new ValueConverter<DateTime, DateTime>(
v => v.ToUniversalTime(),
v => DateTime.SpecifyKind(v, DateTimeKind.Utc)
);
or an extension method when using ADO.net directly
public static DateTime GetDateTimeUtc(this IDataRecord record, int i)
{
return DateTime.SpecifyKind(record.GetDateTime(i), DateTimeKind.Utc);
}
BTW, regarding #3, it use to be possible to create your own culture with CultureAndRegionInfoBuilder.
It does not seem to be possible in net core though: https://github.com/dotnet/runtime/issues/23802
5 things about DateTime time zones and formatting - Code4IT
You're using DateTime.Now, aren't you? Be careful, because it may bring some troubles to your application. Here I'll explain why and I'll talk about time zones and formatting in C# DateTime.
https://www.code4it.dev/blog/5-things-datetime-timezones-and-formatting