user1765862
user1765862

Reputation: 14145

un-representable DateTime

I have method which expects two datetime parameters

public  void SomeReport(DateTime TimeFrom, DateTime TimeTo)
{
    // ommited 
    TimeFrom.ToString("ddMMyy"), TimeTo.ToString("ddMMyy")));
    // ommited
}

When I'm sending this params

 DateTime TimeTo = DateTime.Now;
 DateTime TimeFrom = new DateTime().AddHours(-1);

This error occured:

System.ArgumentOutOfRangeException : The added or subtracted value results in an un-representable DateTime.

What can be the problem?

Upvotes: 27

Views: 65210

Answers (7)

Erdogan
Erdogan

Reputation: 990

In my error, I used the time as 24:00 instead of 00:00

Upvotes: 0

Bahruz Qasimov
Bahruz Qasimov

Reputation: 11

Look you date or time data .There not enough digits for date or time Example date must be 8 digit 20140604 and time 6 digit like this 180203.For this reason you are getiing error. i get this error too and find time 18000 and change this to 180000 problem solved.

Upvotes: 1

user1874915
user1874915

Reputation: 19

In your case TimeFrom holds the datetime from which -1 can not be added. You can either invoke

DateTime TimeFrom = TimeTo .AddHours(-1);

or

DateTime TimeFrom = new DateTime().now.AddHours(-1);

Both of them yield the same result.

Upvotes: 0

Yahia
Yahia

Reputation: 70369

creating a DateTime with new DateTime() gives you a DateTime with DateTime.MinValue... from this you actually can't subtract anything... otherwise you get the exception you got... see MSDN

Upvotes: 2

Oded
Oded

Reputation: 498942

new DateTime() is 01/01/0001 00:00:00 which is also DateTime.MinValue.

You are subtracting one hour from that.

Guessing you are trying to subtract an hour from the TimeTo value:

var TimeFrom = TimeTo.AddHours(-1);

Upvotes: 51

The_Cthulhu_Kid
The_Cthulhu_Kid

Reputation: 1859

try:

DateTime TimeTo = DateTime.Now;
DateTime TimeFrom = TimeTo.AddHours(-1);

Upvotes: 7

Rawling
Rawling

Reputation: 50104

new DateTime() returns the minimum representable DateTime; adding -1 hours to this results in a DateTime that can't be represented.

You probably want DateTime TimeFrom = TimeTo.AddHours(-1);

Upvotes: 14

Related Questions