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 fecha y hora, aplica el formato de fecha y hora requerido a una celda mediante Style.getNumber() o la propiedad Style.Custom . La propiedad CellValue.DateTimeValue retorna el objeto DateTime, que especifica la fecha y hora representada 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:
- 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.
- El sistema de fechas 1904. La primera fecha es 1 de enero de 1904, y su valor serial es 0. La última fecha es 31 de diciembre de 9999, y su valor serial es 2,957,003. Para usar este sistema de fechas en el libro de trabajo, configura la propiedad WorkbookSettings.getDate1904() a true.
Este ejemplo muestra que los valores seriales almacenados en la misma fecha en diferentes sistemas de fecha son diferentes.
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiating an Workbook object | |
let workbook = new AsposeCells.Workbook(); | |
workbook.getSettings().setDate1904(false); | |
// Obtaining the reference of the newly added worksheet | |
let ws = workbook.getWorksheets().get(0); | |
let cells = ws.getCells(); | |
let dateData = new Date(2023, 10, 23); // JavaScript months are 0-based | |
// Setting the DateTime value to the cells | |
let a1 = cells.get("A1"); | |
a1.putValue(dateData); | |
// Check if the cell contains a numeric value | |
if (a1.getType() === AsposeCells.CellValueType.IsNumeric) { | |
console.log("A1 is Numeric Value: " + a1.getDoubleValue()); | |
} | |
workbook.getSettings().setDate1904(true); | |
console.log("use The 1904 date system===================="); | |
// Setting the DateTime value to the cells | |
let a2 = cells.get("A2"); | |
a2.setValue(dateData); | |
// Check if the cell contains a numeric value | |
if (a2.getType() === AsposeCells.CellValueType.IsNumeric) { | |
console.log("A2 is Numeric Value: " + a2.getDoubleValue()); | |
} |
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.
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiating a Workbook object | |
let workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the newly added worksheet | |
let ws = workbook.getWorksheets().get(0); | |
let cells = ws.getCells(); | |
// Setting the DateTime value to the cells | |
let a1 = cells.get("A1"); | |
a1.putValue(new Date()); | |
// Check if the cell contains a numeric value | |
if (a1.getType() === AsposeCells.CellValueType.IsNumeric) { | |
console.log("A1 is Numeric Value: " + a1.isNumericValue()); | |
} | |
let a1Style = a1.getStyle(); | |
// Set custom Datetime style | |
a1Style.setCustom("mm-dd-yy hh:mm:ss"); | |
a1.setStyle(a1Style); | |
// Check if the cell contains a DateTime value | |
if (a1.getType() === AsposeCells.CellValueType.IsDateTime) { | |
console.log("Cell A1 contains a DateTime value."); | |
} else { | |
console.log("Cell A1 does not contain a DateTime value."); | |
} | |
// Setting the DateTime value to the cells | |
let a2 = cells.get("A2"); | |
a2.putValue(new Date()); | |
// Check if the cell contains a numeric value | |
if (a2.getType() === AsposeCells.CellValueType.IsNumeric) { | |
console.log("A2 is Numeric Value: " + a2.isNumericValue()); | |
} | |
let a2Style = a2.getStyle(); | |
// Set the display format of numbers and dates. | |
a2Style.setNumber(22); | |
a2.setStyle(a2Style); | |
// Check if the cell contains a DateTime value | |
if (a2.getType() === AsposeCells.CellValueType.IsDateTime) { | |
console.log("Cell A2 contains a DateTime value."); | |
} else { | |
console.log("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.
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the newly added worksheet | |
const ws = workbook.getWorksheets().get(0); | |
const cells = ws.getCells(); | |
// Setting the DateTime value to the cells | |
const a1 = cells.get("A1"); | |
a1.putValue(new Date()); | |
// Check if the cell contains a numeric value | |
if (a1.getType() === AsposeCells.CellValueType.IsNumeric) { | |
console.log("A1 is Numeric Value: " + a1.isNumericValue); | |
} | |
let a1Style = a1.getStyle(); | |
// Set custom Datetime style | |
a1Style.setCustom("mm-dd-yy hh:mm:ss"); | |
a1.setStyle(a1Style); | |
// Check if the cell contains a DateTime value | |
if (a1.getType() === AsposeCells.CellValueType.IsDateTime) { | |
console.log("Cell A1 contains a DateTime value."); | |
// Get the DateTime value | |
const dateTimeValue = a1.getDateTimeValue(); | |
// Now, you can use dateTimeValue as needed | |
console.log("A1 DateTime Value: " + dateTimeValue); | |
// Output date formatted string | |
console.log("A1 DateTime String Value: " + a1.getStringValue()); | |
} else { | |
console.log("Cell A1 does not contain a DateTime value."); | |
} | |
// Setting the DateTime value to the cells | |
const a2 = cells.get("A2"); | |
a2.putValue(new Date()); | |
// Check if the cell contains a numeric value | |
if (a2.getType() === AsposeCells.CellValueType.IsNumeric) { | |
console.log("A2 is Numeric Value: " + a2.isNumericValue); | |
} | |
let a2Style = a2.getStyle(); | |
// Set the display format of numbers and dates. | |
a2Style.setNumber(22); | |
a2.setStyle(a2Style); | |
// Check if the cell contains a DateTime value | |
if (a2.getType() === AsposeCells.CellValueType.IsDateTime) { | |
console.log("Cell A2 contains a DateTime value."); | |
// Get the DateTime value | |
const dateTimeValue = a2.getDateTimeValue(); | |
// Now, you can use dateTimeValue as needed | |
console.log("A2 DateTime Value: " + dateTimeValue); | |
// Output date formatted string | |
console.log("A2 DateTime String Value: " + a2.getStringValue()); | |
} else { | |
console.log("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