TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# FromOADate and Excel Dates

This C# article shows how to use the DateTime.FromOADate method. It provides example code.

FromOADate. Excel interop dates are stored as integers.

It is possible to convert them to DateTimes. We use FromOADate to convert OLE Automation Dates to real DateTime values. We examine the FromOADate method.

Input OLE date:

39456
39482
39484
36526
36557
39270

Output DateTime:

1/9/2008
2/4/2008
2/6/2008
1/1/2000
2/1/2000
7/7/2007

Example. First, you can use FromOADate as a static method on the DateTime type. This method receives one double value as its parameter, and the returns a full DateTime. This will convert those five-digit integers to the regular DateTimes.

Next: We show some example calls and output. With Console.WriteLine, we print the results.

Console.WriteLine

C# program that uses FromOADate

using System;

class Program
{
    static void Main()
    {
	var arr = new string[]
	{
	    // OA Dates in early 2008.
	    "39456", // (1/9/2008)
	    "39482",
	    "39484",
	    "39493",
	    "39496",
	    "39497",
	    "39501",
	    "39506",
	    "39510",
	    "39513",
	    "39515",
	    "39521", // (3/14/2008)
	    // Different series starts here.
	    "36526", // (1/1/2000)
	    "36557", // (2/1/2000)
	    "39270"  // (7//7/2007)
	};

	foreach (string b in arr)
	{
	    // We must have a double to convert the OA date to a real date.
	    double d = double.Parse(b);

	    // Get the converted date from the OLE automation date.
	    DateTime conv = DateTime.FromOADate(d);

	    // Write to console.
	    Console.WriteLine("{0} = {1}",
		b,
		conv.ToShortDateString());
	}
    }
}

Output

39456 = 1/9/2008
39482 = 2/4/2008
39484 = 2/6/2008
39493 = 2/15/2008
39496 = 2/18/2008
39497 = 2/19/2008
39501 = 2/23/2008
39506 = 2/28/2008
39510 = 3/3/2008
39513 = 3/6/2008
39515 = 3/8/2008
39521 = 3/14/2008
36526 = 1/1/2000
36557 = 2/1/2000
39270 = 7/7/2007

It shows a sample array. Above, the array is simply an example of OADates you might encounter. Typically the doubles will be somewhere in the 30,000s, but you may encounter outliers.

Next, it loops with foreach through each example string. We use a foreach loop to look at each number in the array. These numbers would be typically be extracted from Microsoft Excel Interop or some databases.

Foreach

Values must be double types. The FromOADate method requires that it receive a double value. For the custom code above, we convert the string to a double. You may not need to do this.

Double

Finally: We use the static DateTime.FromOADate to turn the double into a regular DateTime.

Static Method

Summary. We used the FromOADate method on the DateTime type in the C# language. We saw the input and output. This is a tricky topic and this article serves as an adjunct to my other Excel Interop article.

Excel

Note: These methods are mainly required for legacy XLS files, which may be replaced with more robust XLSX XML files.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf