4

I have the following code in an Windows Store Application using C# :

birthDateTimePicker.Date = DateTime.Now;
if (birthDateTimePicker.Date == DateTime.MinValue)
{
    no_date_lable.Visibility = Visibility.Collapsed;
    birthDateTimePicker.Visibility = Visibility.Collapsed;
}

Note : this line birthDateTimePicker.Date = DateTime.Now; is for example really user will selected or it will come from server.

But i get an exception in this line :

if (dt_born_dt == DateTime.MinValue)

What is the reason for the exception thrown :

The UTC time represented when the offset is applied must be between year 0 and 10,000.

Really i tested this in three computers and i got error in one of them!!!

I changed the time zone of my system to (UTC-08:00) Pacific Time (US & Canada) and didn't get this exception at all and codes worked correctly!

My question is really why this exception occurred in the system that its time zone was (UTC+03:30) Tehran!

Sorry for my poor english

enter image description here

2 Answers2

4

So your control dt_born_dt (please use meaningful names like birthDateTimePicker, and consider that birthdates don't need a time and most certainly can't be of year 1 [there are no people of age ~2015 alive right now], perhaps use a nullable type) has a Date property that is of type DateTimeOffset, which is visible from the +3:30 value the debugger shows.

Now this type has an implicit conversion from DateTime, so if you do this:

birthDatePicker.Date = DateTime.MinValue;

It'll convert the DateTime.MinValue, which has a DateTimeKind.Unspecified kind, to UTC. This will throw the exception you show, because you're in a GMT+ timezone: it'll subtract your GMT offset from the MinValue, yielding an invalid value, as explained in Converting DateTime.MinValue to DateTimeOffset.

The solution: use DateTimeOffset.MinValue.

Also, don't use try-catch around Parse(Exact); use TryParse(Exact).

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • thank you, i used this DateTimeOffset.MinValue but i cant use birthDatePicker.Date = DateTime.MinValue; the exception occurred again when i write this line after my code birthDatePicker.Date = DateTime.MinValue; – FatemehEbrahimiNik Dec 23 '15 at 09:52
  • Yeah because you should use `birthDatePicker.Date = DateTimeOffset.MinValue`. – CodeCaster Dec 23 '15 at 09:53
  • Again, create a [mcve]. From your description it is unclear what you're trying to do. `DateTimeOffset someDateTimeOffset = DateTimeOffset.MinValue` will compile and run just fine. – CodeCaster Dec 23 '15 at 09:56
  • 1
    Correct. Here is the [documentation](https://msdn.microsoft.com/en-us/library/system.datetimeoffset.op_implicit.aspx) for that `implicit operator`. The asker should have included a stack trace; then the cause would have been easier to determine. – Jeppe Stig Nielsen Dec 23 '15 at 10:04
2

@CodeCaster answer was very complete and helpful. However, another way to tackle this problem is to convert the "DateTime.MinValue" to UTC.

if (birthDateTimePicker.Date == DateTime.MinValue.ToUniversalTime())

I had to use this method because "birthDateTimePicker.Date" type in my DB is DateTime and not DateTimeOffset, and I did't want to convert it.

Thank you all my friends.

Hedeshy
  • 1,266
  • 2
  • 15
  • 24
  • This is not a good solution. Maybe it works on your machine (whose clock is ahead of Greenwich, western hemisphere) because `ToUniversalTime()` avoids exceptions. But will it work on the western hemisphere? In zone -06:00, say, will this create a date and time corresponding to 0001/01/01 00:06:00? – Jeppe Stig Nielsen Dec 23 '15 at 16:22
  • @JeppeStigNielsen I tested many of timezone and didn't get exception, its saved in my DB with 0001/01/01 00:00:00,did i make a mistake? – FatemehEbrahimiNik Dec 24 '15 at 04:59