System.DateTime without range check

  • Thread starter Thread starter Abdolhosein Vakilzadeh Ebrahimi
  • Start date Start date
A

Abdolhosein Vakilzadeh Ebrahimi

Is there any way to construct a System.DateTime which doesn't check
year, month and day to be in correct range== no validation?

Thanks
A.V.Ebrahimi
 
I need it to store persian ( farsi ) dates, which days per month are
different from georgians.

Also would you please let me know how to install farsi calendar on
Windows?

Thanks
 
Hi Abdolhosein,

Abdolhosein Vakilzadeh Ebrahimi said:
I need it to store persian ( farsi ) dates, which days per month are
different from georgians.

System.DateTime can optionally take a System.Globalization.Calendar object
as an argument to its constructor. Calendar objects contain information
about such things as (a) how many months there are; (b) how long each month
is; (c) when leap years occur, etc.

The default calendar is System.Globalization.GregorianCalendar, and as you
noticed, System.DateTime will throw an exception if you try to set it to,
e.g., month 2, day 31 ("Ordibehesht 31st" in the Farsi/Jalaali calendar,
which a legal date). This is because System.Globalization.GregorianCalendar
tells System.DateTime that month 2 has only 28 (sometimes 29) days.

Unfortunately the versions of the .NET Framework that have shipped so far do
not include a definition for the Jalaali calendar. (Although there will be
a System.Globalization.JalaaliCalendar class in the next version.) So you
basically have two choices, neither of which is very appealing:

1. You create your own JalaaliCalendar class (deriving from
System.Globalization.Calendar) and pass this to System.DateTime.

2. You avoid using the System.DateTime.AddDays(), AddMonths(), etc., methods
(as well as the System.DateTime constructors that take year, month or day
parameters) and operate exclusively over ticks (100-nanosecond intervals).
You can construct System.DateTime objects by passing a tick value and you
can use the AddTicks() method to modify existing ones. Internally
System.DateTime represents points in time as the number of ticks that have
elapsed since 12:00:00 midnight, January 1, 0001 A.D. You could create your
own logic for converting Jalaali dates into ticks and operate through that
"translation layer".

The first solution is definitely better if you can manage it.
Also would you please let me know how to install farsi calendar on
Windows?

I'm not sure exactly what you mean by this, but if you install support for
Farsi through the Control Panel | Regional and Language Settings |
Supplemental Language Support | Install files for complex script and
right-to-left languages you should also get the Farsi standards and formats
installed (numbers, currency, time, date formats). You can select your
preference for these formats through Control Panel | Regional and Language
Settings | Standards and Formats. (All of this information applies to
Windows XP.)

Good luck,
Nick
 
I meant how to install Jalaali calendar in windows, if there is any.

Thanks for quick reply.
 
Back
Top