How to convert dateTimeOffset from Hijri into Gregorian without changing current thread culture?

c# datetimeoffset entity-framework-core

Question

When I get the value of a dateTimeOffset column from the database using EntityFrameworkCore I get the value of that column in Hijri when CurrentCulture & CurrentUICulture of the current thread is Arabic(ar-SA).

I know that if I changed the CurrentCulture of CurrentThread before returning that value I would get it in Gregorian calendar.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-UK");

But how can I return it as dateTimeOffset in Gregorian calendar without changing CurrentCulture of the CurrentThread?

1
0
12/12/2018 7:44:05 AM

Popular Answer

Your assertion that the current culture affects how the EF Core creates the DateTimeOffset instance is incorrect. DateTimeOffset and DateTime data types do not have a calendar unto themselves, and the thread's current culture does not affect how they are created (unless parsing from a string).

However, some instance methods, such as ToString, will use the current culture when they are called, unless otherwise specified. Likely you are seeing string output created by ToString (whether explicitly or implicitly), and seeing the thread's current culture affecting the output there.

Therefore, simply pass a specific culture (often the InvariantCulture) to the ToString method, and you will see the output in the Gregorian calendar.

string myString = myDateTimeOffset.ToString(CultureInfo.InvariantCulture);
1
12/26/2018 8:11:19 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow