I'm trying to create a date from a TimeSpan
that has 100 hours and 100 minutes but i'm gettin the error:
Run-time exception (line 9): Not a legal OleAut date.
Stack Trace:
[System.OverflowException: Not a legal OleAut date.] at System.DateTime.TicksToOADate(Int64 value)
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
TimeSpan timeSpan = TimeSpan.FromHours(100) + TimeSpan.FromMinutes(100);
var OADate = DateTime.FromOADate((DateTime.MinValue.AddTicks(timeSpan.Ticks)).ToOADate());
}
}
This is to link in with a legacy value in the db from vb6.
It works with small hour and minutes values but fails with this, any ideas?
Created a Fiddle
Let's have a look at the date you are trying to convert into OADate
:
TimeSpan timeSpan = TimeSpan.FromHours(100) + TimeSpan.FromMinutes(100);
var OADate = DateTime.MinValue.AddTicks(timeSpan.Ticks);
Console.WriteLine(OADate.ToString("d MMMM yyyy HH:mm:ss", CultureInfo.InvariantCulture));
You'll get this
5 January 0001 05:40:00
But according to the manual (bold is mine)
The base OLE Automation Date is midnight, 30 December 1899. The minimum OLE Automation date is midnight, 1 January 0100. The maximum OLE Automation Date is the same as DateTime.MaxValue, the last moment of 31 December 9999.
that's why DateTime.MinValue.AddTicks(timeSpan.Ticks).ToOADate()
is illegal (OADate is below the minimum) and you have the exception thrown.