How to get “DateTime.Now” in the Gregorian format when client calendar is non-Gregorian?
-
02-07-2021 - |
Question
How can I get DateTime.Now
in Gregorian calender when client calendar is non-Gregorian?
My project is a Silverlight with C# code and client default calender is not predicted.
Solution
A DateTime value has no format. Internally it's just a count of ticks since the epoch, 0001-01-01 00:00 in .NET. Formatting is when you call ToString.
Just use a different CultureInfo for the culture that has the calendar you want, e.g.
CultureInfo french = CultureInfo.GetCultureInfo("fr-FR");
String nowStr = DateTime.Now.ToString( french );
Use CultreInfo.InvariantCulture
if you just to use a predictable Western culture with a Gregorian calendar.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow