Dates and DateTime

Range["A1"].Value = "08/11/04" 

This value could be interpreted as any of the following dates depending on the locale of the current thread

  • August 11, 2004

  • November 8, 2004

  • November 4, 2008


The workaround is to never pass dates as literal strings into Excel.
Always construct a date using the System.DateTime object and pass it to Excel using the DateTime.ToOADate method
The ToOADate method converts a DateTime to an OLE Automation date which is the date format used by Excel.


System.DateTime odate = new System.DateTime(2014,4,11); 
range1.Value = odate.ToOADate();


Excel calculates the number of days elapsed since 31 December 1899
.NET calculates the number of days elapsed since 1 January 1900
To convert an Excel date in 1900 that is earlier than 1 March 1900 into a DateTime object you must add one day


Excel cannot represent days before 1 January 1900 or (I January 1904) so when converting a DateTime into an Excel date, you have to pass a string rather than a number representing the date.




© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext