Wie man Daten und Zeiten verwaltet

Wie man Daten und Zeiten in Excel speichert

Daten und Zeiten werden in Zellen als Zahlen gespeichert. Daher sind die Werte von Zellen, die Daten und Zeiten enthalten, vom numerischen Typ. Eine Zahl, die ein Datum und eine Uhrzeit angibt, besteht aus den Datum (Ganzzahlteil) und Uhrzeit (Bruchteilteil) Komponenten. Die Eigenschaft Cell.DoubleValue gibt diese Zahl zurück.

Wie man Daten und Zeiten in Aspose.Cells anzeigt

Um eine Zahl als Datum und Uhrzeit anzuzeigen, wenden Sie das erforderliche Datums- und Uhrzeitformat auf eine Zelle an, entweder über Style.getNumber() oder Style.Custom. Die CellValue.DateTimeValue-Eigenschaft liefert das DateTime-Objekt, das das Datum und die Uhrzeit angibt, welche durch die Zahl in einer Zelle dargestellt werden.


Wie man zwischen zwei Datumsystemen in Aspose.Cells wechselt

MS-Excel speichert Daten als Zahlen, die als Serienwerte bezeichnet werden. Ein Serienwert ist eine Ganzzahl, die die Anzahl der vergangenen Tage seit dem ersten Tag im Datensystem angibt. Excel unterstützt die folgenden Datensysteme für Serienwerte:

  1. Das 1900-Datensystem. Das erste Datum ist der 1. Januar 1900 und sein Serienwert ist 1. Das letzte Datum ist der 31. Dezember 9999 und sein Serienwert beträgt 2.958.465. Dieses Datensystem wird standardmäßig in der Arbeitsmappe verwendet.
  2. Das 1904-Datumsystem. Das erste Datum ist der 1. Januar 1904, und sein Serienwert ist 0. Das letzte Datum ist der 31. Dezember 9999, und sein Serienwert ist 2.957.003. Um dieses Datumsystem im Arbeitsbuch zu verwenden, setzen Sie die Eigenschaft WorkbookSettings.getDate1904() auf true.

Dieses Beispiel zeigt, dass die in verschiedenen Datensystemen gespeicherten Serienwerte für dasselbe Datum unterschiedlich sind.

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());
}

Ausgabenergebnis:

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

So legen Sie den DateTime-Wert in Aspose.Cells fest

Dieses Beispiel legt einen DateTime-Wert in Zelle A1 und A2 fest, legt ein benutzerdefiniertes Format für A1 und ein Zahlenformat für A2 fest und gibt dann die Werttypen aus.

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

Ausgabenergebnis:

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

So erhalten Sie den DateTime-Wert in Aspose.Cells

Dieses Beispiel legt einen DateTime-Wert in Zelle A1 und A2 fest, legt ein benutzerdefiniertes Format für A1 und ein Zahlenformat für A2 fest, überprüft die Werttypen von zwei Zellen und gibt dann den DateTime-Wert und den formatierten String aus.

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

Ausgabenergebnis:

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