ExcelSerialDate Class
Definition
- Assembly
- Bodu.Formats.Excel.Binary.dll
- Package
- Bodu.Formats.Excel.Binary 1.0.0
- Source
- ExcelSerialDate.cs
Converts Excel serial date numbers into calendar dates.
public static class ExcelSerialDate
- Inheritance
-
ExcelSerialDate
- Inherited Members
Remarks
Excel stores dates as floating-point serial numbers measured from the epoch 1899-12-30, deliberately
preserving the historical 1900 leap-year bug so that values round-trip with Excel. The reader never infers that a
numeric cell is a date; this helper is provided so a caller that knows a column holds dates can convert its values.
Methods
FromSerialDate(double)
Converts an Excel serial date number in the 1900 date system to a DateOnly, discarding any fractional time-of-day component.
public static DateOnly FromSerialDate(double serial)
Parameters
serialdoubleThe Excel serial date number.
Returns
- DateOnly
The calendar date represented by
serial.
Exceptions
- ArgumentException
Thrown when
serialis outside the range of representable OLE Automation dates.
FromSerialDate(double, ExcelDateSystem)
Converts an Excel serial date number to a DateOnly using the specified date system, discarding any fractional time-of-day component.
public static DateOnly FromSerialDate(double serial, ExcelDateSystem dateSystem)
Parameters
serialdoubleThe Excel serial date number.
dateSystemExcelDateSystemThe date system that establishes the serial number's epoch.
Returns
- DateOnly
The calendar date represented by
serial.
Exceptions
- ArgumentException
Thrown when
serialis outside the range of representable OLE Automation dates.
ToDateTime(double)
Converts an Excel serial date number in the 1900 date system to a DateTime, preserving any fractional time-of-day component.
public static DateTime ToDateTime(double serial)
Parameters
serialdoubleThe Excel serial date number.
Returns
- DateTime
The date and time represented by
serial.
Examples
using Bodu.Formats.Excel;
// Serial 25569 is 1970-01-01 in the default 1900 date system.
DateTime epoch = ExcelSerialDate.ToDateTime(25569);
Exceptions
- ArgumentException
Thrown when
serialis outside the range of representable OLE Automation dates.
ToDateTime(double, ExcelDateSystem)
Converts an Excel serial date number to a DateTime using the specified date system, preserving any fractional time-of-day component.
public static DateTime ToDateTime(double serial, ExcelDateSystem dateSystem)
Parameters
serialdoubleThe Excel serial date number.
dateSystemExcelDateSystemThe date system that establishes the serial number's epoch.
Returns
- DateTime
The date and time represented by
serial.
Examples
using Bodu.Formats.Excel;
// Convert a date-formatted numeric cell using the workbook's own date system.
using var workbook = ExcelBinaryWorkbook.OpenRead("report.xls");
using ExcelWorksheetReader reader = workbook.OpenWorksheet(0);
while (reader.TryReadCell(out ExcelCell cell))
{
if (cell.IsDateFormatted && cell.NumberValue is double serial)
Console.WriteLine(ExcelSerialDate.ToDateTime(serial, workbook.DateSystem));
}
Exceptions
- ArgumentException
Thrown when
serialis outside the range of representable OLE Automation dates.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |