Today is 1397/02/29 (yyyy/mm/dd) in Persian calendar. and my website reports his error:
'Year, Month, and Day parameters describe an un-representable DateTime.'
I used datetime2 in SQL database and saved Persian date in it by converting this date:
PersianCalendar pc = new PersianCalendar();
DateTime dateTime = DateTime.Now;
return new DateTime(pc.GetYear(dateTime), pc.GetMonth(dateTime), pc.GetDayOfMonth(dateTime), pc.GetHour(dateTime), pc.GetMinute(dateTime), pc.GetSecond(dateTime), 0);
What should i do?
Quite ignorant here... but in general you should treat "internally" (in the program and in the db) all the dates using the normal Proleptic Gregorian calendar (per ISO 8601), and reformat them to Persian only when you read input from the user/write output to the user. There is a PersianCalendar class in .NET that helps converting persian-to-american and back.
For example
works correctly, but if you take a look at the dt variable you'll see that it shows as 2018-05-19.
If doing a
ToString()
on aDateTime
variable you see a persian date, then you probably have an error in your code. If doing aSELECT
on a table in the db you see a persian date, then you probably have an error in your db.The interesting paradox is that internally both .NET and SQL save dates as a single number, representing how many units of time (where in .NET a unit of time is 100 nanoseconds, in SQL it is 1 day) have passed from a "zero point" to the date, but then all their "standard" methods for handling dates (both in .NET and SQL) use the gregorian calendar (because it is the most used in the world).