DateTime.Now in C# must return '11/30/2019 11:10:12 AM' but it returns '09/09/1398 11:10:12 ق.ظ' in Asp.Net web Applications i have this problem, but in C# console application does not any problem.
DateTime dateTime = DateTime.Now;
DateTime.Now in C# must return '11/30/2019 11:10:12 AM' but it returns '09/09/1398 11:10:12 ق.ظ' in Asp.Net web Applications i have this problem, but in C# console application does not any problem.
DateTime dateTime = DateTime.Now;
The DateTime
type itself is always in the Gregorian calendar, in terms of the values returned by Year
, Month
and Day
.
What you're observing is the result of formatting the DateTime
, which converts it in to the default calendar system for the default thread culture. The simplest way to fix this is to use the invariant culture:
using System;
using System.Globalization;
class Test
{
static void Main()
{
var dateTime = DateTime.Now;
string formatted = dateTime.ToString(
"yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(formatted); // 2019-11-30 09:19:43 or similar
}
}
You could also try this to force it to return DateTime
with the standard US culture info.
CultureInfo MyCultureInfo = new CultureInfo("en-US");
DateTime MyNewDate = DateTime.Parse(DateTime.Now.ToString(), MyCultureInfo);