Cómo Gestionar Fechas y Horas

Cómo almacenar fechas y horas en Excel

Las fechas y horas se almacenan en las celdas como números. Por lo tanto, los valores de las celdas que contienen fechas y horas son de tipo numérico. Un número que especifica una fecha y hora consta de los componentes de la fecha (parte entera) y la hora (parte fraccionaria). La propiedad Cell.DoubleValue devuelve este número.

Cómo mostrar fechas y horas en Aspose.Cells

Para mostrar un número como una fecha y hora, aplica el formato de fecha y hora requerido a una celda a través de la propiedad Style.Number o Style.Custom. La propiedad CellValue.DateTimeValue devuelve el objeto DateTime, que especifica la fecha y hora representadas por el número contenido en una celda.

Cómo cambiar dos sistemas de fecha en Aspose.Cells

MS-Excel almacena fechas como números que se llaman valores seriales. Un valor serial es un entero que representa el número de días transcurridos desde el primer día en el sistema de fecha. Excel admite los siguientes sistemas de fecha para los valores seriales:

  1. El sistema de fecha 1900. La primera fecha es el 1 de enero de 1900 y su valor serial es 1. La última fecha es el 31 de diciembre de 9999 y su valor serial es 2,958,465. Este sistema de fecha se utiliza en el libro de trabajo de forma predeterminada.
  2. El sistema de fecha 1904. La primera fecha es el 1 de enero de 1904 y su valor serial es 0. La última fecha es el 31 de diciembre de 9999 y su valor serial es 2,957,003. Para utilizar este sistema de fecha en el libro de trabajo, establece la propiedad Workbook.Settings.Date1904 en true.

Este ejemplo muestra que los valores seriales almacenados en la misma fecha en diferentes sistemas de fecha son diferentes.

//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);
}
Resultado de la salida:

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

Cómo establecer el valor de fecha y hora en Aspose.Cells

Este ejemplo establece un valor de fecha y hora en la celda A1 y A2, establece el formato personalizado de A1 y el formato numérico de A2, y luego muestra los tipos de valor.

//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.");
}

Resultado de la salida:

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

Cómo obtener el valor de fecha y hora en Aspose.Cells

Este ejemplo establece un valor de fecha y hora en la celda A1 y A2, establece el formato personalizado de A1 y el formato numérico de A2, comprueba los tipos de valor de las dos celdas, y luego muestra el valor de fecha y hora y la cadena formateada.

//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.");
}

Resultado de la salida:

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