How to Manage Dates and Times

How to store Dates and Times in Excel

Dates and times are stored in cells as numbers. Thus, the values of cells that contain dates and times are of the numeric type. A number that specifies a date and time consists of the date (integer part) and time (fractional part) components. The Cell.DoubleValue property returns this number.

How to Display Dates and Times in Aspose.Cells

To display a number as a date and time, apply the required date and time format to a cell via the Style.Number or Style.Custom property. The CellValue.DateTimeValue property returns the DateTime object, which specifies the date and time that is represented by the number contained in a cell.

How to switch two date systems in Aspose.Cells

MS-Excel stores dates as numbers that are called serial values. A serial value is an integer that is the number of elapsed days from the first day in the date system. Excel supports the following date systems for serial values:

  1. The 1900 date system. The first date is January 1, 1900, and its serial value is 1. The last date is December 31, 9999, and its serial value is 2,958,465. This date system is used in the workbook by default.
  2. The 1904 date system. The first date is January 1, 1904, and its serial value is 0. The last date is December 31, 9999, and its serial value is 2,957,003. To use this date system in the workbook, set the Workbook.Settings.Date1904 property to true.

This example shows that the serial values stored on the same date in different date systems are different.

//Instantiating an Workbook object
Workbook workbook = new Workbook();
workbook.Settings.Date1904 = false;
//Obtaining the reference of the newly added worksheet
Worksheet ws = workbook.Worksheets[0];
Cells cells = ws.Cells;
DateTime dateData = new DateTime(2023, 11, 23);
//Setting the DateTime value to the cells
Cell a1 = cells["A1"];
a1.PutValue(dateData);
// Check if the cell contains a numeric value
if (a1.Type == CellValueType.IsNumeric)
{
Console.WriteLine("A1 is Numeric Value: " + a1.DoubleValue);
}
workbook.Settings.Date1904 = true;
Console.WriteLine("use The 1904 date system====================");
//Setting the DateTime value to the cells
Cell a2 = cells["A2"];
a2.Value = dateData;
// Check if the cell contains a numeric value
if (a2.Type == CellValueType.IsNumeric)
{
Console.WriteLine("A2 is Numeric Value: " + a2.DoubleValue);
}
Output result:

A1 is Numeric Value: 45253
use The 1904 date system====================
A2 is Numeric Value: 43791

How to Set DateTime Value in Aspose.Cells

This example sets a DateTime value in cell A1 and A2, sets custom format of A1 and number format of A2, and then outputs the value types.

//Instantiating an Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the newly added worksheet
Worksheet ws = workbook.Worksheets[0];
Cells cells = ws.Cells;
//Setting the DateTime value to the cells
Cell a1 = cells["A1"];
a1.PutValue(DateTime.Now);
// Check if the cell contains a numeric value
if (a1.Type == CellValueType.IsNumeric)
{
Console.WriteLine("A1 is Numeric Value: " + a1.IsNumericValue);
}
Style a1Style = a1.GetStyle();
// Set custom Datetime style
a1Style.Custom = "mm-dd-yy hh:mm:ss";
a1.SetStyle(a1Style);
// Check if the cell contains a DateTime value
if (a1.Type == CellValueType.IsDateTime)
{
Console.WriteLine("Cell A1 contains a DateTime value.");
}
else
{
Console.WriteLine("Cell A1 does not contain a DateTime value.");
}
//Setting the DateTime value to the cells
Cell a2 = cells["A2"];
a2.Value = DateTime.Now;
// Check if the cell contains a numeric value
if (a2.Type == CellValueType.IsNumeric)
{
Console.WriteLine("A2 is Numeric Value: " + a2.IsNumericValue);
}
Style a2Style = a2.GetStyle();
// Set the display format of numbers and dates.
a2Style.Number = 22;
a2.SetStyle(a2Style);
// Check if the cell contains a DateTime value
if (a2.Type == CellValueType.IsDateTime)
{
Console.WriteLine("Cell A2 contains a DateTime value.");
}
else
{
Console.WriteLine("Cell A2 does not contain a DateTime value.");
}

Output result:

A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A2 is Numeric Value: True
Cell A2 contains a DateTime value.

How to Get DateTime Value in Aspose.Cells

This example sets a DateTime value in cell A1 and A2, sets custom format of A1 and number format of A2, checks the value types of two cells, and then outputs the DateTime value and formatted string.

//Instantiating an Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the newly added worksheet
Worksheet ws = workbook.Worksheets[0];
Cells cells = ws.Cells;
//Setting the DateTime value to the cells
Cell a1 = cells["A1"];
a1.PutValue(DateTime.Now);
// Check if the cell contains a numeric value
if (a1.Type == CellValueType.IsNumeric)
{
Console.WriteLine("A1 is Numeric Value: " + a1.IsNumericValue);
}
Style a1Style = a1.GetStyle();
// Set custom Datetime style
a1Style.Custom = "mm-dd-yy hh:mm:ss";
a1.SetStyle(a1Style);
// Check if the cell contains a DateTime value
if (a1.Type == CellValueType.IsDateTime)
{
Console.WriteLine("Cell A1 contains a DateTime value.");
// Get the DateTime value
DateTime dateTimeValue = a1.DateTimeValue;
// Now, you can use dateTimeValue as needed
Console.WriteLine("A1 DateTime Value: " + dateTimeValue);
// Output date formatted string
Console.WriteLine("A1 DateTime String Value: " + a1.StringValue);
}
else
{
Console.WriteLine("Cell A1 does not contain a DateTime value.");
}
//Setting the DateTime value to the cells
Cell a2 = cells["A2"];
a2.Value = DateTime.Now;
// Check if the cell contains a numeric value
if (a2.Type == CellValueType.IsNumeric)
{
Console.WriteLine("A2 is Numeric Value: " + a2.IsNumericValue);
}
Style a2Style = a2.GetStyle();
// Set the display format of numbers and dates.
a2Style.Number = 22;
a2.SetStyle(a2Style);
// Check if the cell contains a DateTime value
if (a2.Type == CellValueType.IsDateTime)
{
Console.WriteLine("Cell A2 contains a DateTime value.");
// Get the DateTime value
DateTime dateTimeValue = a2.DateTimeValue;
// Now, you can use dateTimeValue as needed
Console.WriteLine("A2 DateTime Value: " + dateTimeValue);
// Output date formatted string
Console.WriteLine("A2 DateTime String Value: " + a2.StringValue);
}
else
{
Console.WriteLine("Cell A2 does not contain a DateTime value.");
}

Output result:

A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A1 DateTime Value: 11/23/2023 5:59:09 PM
A1 DateTime String Value: 11-23-23 17:59:09
A2 is Numeric Value: True
Cell A2 contains a DateTime value.
A2 DateTime Value: 11/23/2023 5:59:09 PM
A2 DateTime String Value: 11/23/2023 17:59