Adding Seconds to DateTime with a Valid Double Results in ArgumentOutOfRangeException

As others have said, the problem is that the value is too large.

Having looked over it, I believe it represents milliseconds since the Unix epoch, not seconds so you want:

DateTime dt = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);
double d = double.Parse("1332958778172");  // Or avoid parsing if possible :)
Console.Write(dt.AddMilliseconds(d));

Either that, or divide by 1000 before calling AddSeconds - but obviously that will lose data.


The value you are adding results in a date outside of the valid range of dates that a DateTime supports.

DateTime supports 01/01/0001 00:00:00 to 31/12/9999 23:59:59.

A simple calculation of 1332958778172/3600/24/365 gives 42267 years.


I think the double value is genuinely too large. It represents just over 42,267 years (if my maths is correct), and DateTime.MaxValue is 23:59:59.9999999, December 31, 9999

Tags:

C#

.Net