Convert Time-Formatted Column in Excel to C# Datetime

Convert time-formatted column in excel to C# DateTime

As FrankPI said, use DateTime.FromOADate(). You would use this function with the raw data from an Excel cell - there is no need to parse the format.

Excel encodes its dates and times in a double. The integral portion represents the days after January 1, 1900. The fraction part represents the time since midnight of the day referenced. For example:

1.5 is January 1, 1900 @ Noon

and

41507.25 = August 21, 2013 @ 6:00 am

Refer to the MSDN docs on this function for more information:

http://msdn.microsoft.com/en-us/library/system.datetime.fromoadate.aspx

Convert String to datetime [Wed Mar 01, 2017 6:00 AM] this format in c#

You can specify custom format:

DateTime finaldate = DateTime.ParseExact(mydate, "ddd MMM dd, yyyy h:mm tt", CultureInfo.InvariantCulture);

Date Format - C# - Conversion

You can convert it into a DateTime by using the FromOADate method in DateTime. However, that method accepts a double which doesn't work with the current formatting, parse it as a double and set the culture settings like this:

string output = "38876,588587963";
DateTime dt = DateTime.FromOADate(double.Parse(output, CultureInfo.CurrentCulture));

Or if you know the culture is going to always be fr-CA you can do:

DateTime dt = DateTime.FromOADate(double.Parse(output, CultureInfo.CreateSpecificCulture("fr-CA")));

Excel treating date column as general when exporting to Excel using C#

To Set text as DateTime format in epplus you need to set the format type for a cells

EG:

1) For simple short date Pattern

 workSheet.Cells[row, 3].Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;

2) If you want specific format like datetime with time,hour etc then

workSheet.Column(dateColIndex).Style.Numberformat.Format = "mm/dd/yyyy hh:mm:ss AM/PM";


Related Topics



Leave a reply



Submit