كيفية إدارة التواريخ والأوقات
كيفية تخزين التواريخ والأوقات في إكسل
يتم تخزين التواريخ والأوقات في الخلايا على أنها أرقام. وبالتالي، قيم الخلايا التي تحتوي على تواريخ وأوقات تكون من نوع رقمي. الرقم الذي يحدد تاريخًا ووقتًا يتكون من مكونات التاريخ (الجزء الصحيح) والوقت (الجزء الكسري). يُرجع خاصية Cell.DoubleValue هذا الرقم.
كيفية عرض التواريخ والأوقات في Aspose.Cells
لعرض رقم كتاريخ ووقت، قم بتطبيق تنسيق التاريخ والوقت المطلوب على خلية عبر خاصية Style.getNumber() أو Style.Custom، تُعيد خاصية CellValue.DateTimeValue كائن DateTime الذي يحدد التاريخ والوقت الذي يمثله الرقم الموجود في الخلية.
كيفية التبديل بين نظامي التواريخ في Aspose.Cells
تخزن برنامج MS-Excel التواريخ كأرقام تسمى قيم متسلسلة. قيمة متسلسلة هي عدد صحيح هو عدد الأيام المنقضية من اليوم الأول في نظام التاريخ. يدعم Excel الأنظمة التالية للقيم المتسلسلة للتواريخ:
- نظام التاريخ 1900. اليوم الأول هو 1 يناير 1900، وقيمته المتسلسلة هي 1. أما آخر يوم فهو 31 ديسمبر 9999، وقيمته المتسلسلة هي 2،958،465. يُستخدم هذا النظام في جدول العمل افتراضيًا.
- نظام التاريخ 1904. التاريخ الأول هو 1 يناير 1904، وقيمته التسلسلية 0. والتاريخ الأخير هو 31 ديسمبر 9999، وقيمته التسلسلية هي 2,957,003. لاستخدام هذا النظام في مصنف العمل، قم بضبط خاصية WorkbookSettings.getDate1904() على true.
تُظهر هذا المثال أن القيم المتسلسلة المخزنة في نفس التاريخ في أنظمة تاريخ مختلفة هي مختلفة.
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()); | |
} |
النتيجة المخرجة:
A1 is Numeric Value: 45253
use The 1904 date system====================
A2 is Numeric Value: 43791
كيفية تعيين قيمة تاريخ ووقت في Aspose.Cells
يُعين هذا المثال قيمة DateTime في الخلية A1 و A2، يضبط تنسيق مخصص ل A1 وتنسيق رقمي ل A2، ثم يخرج أنواع القيمة.
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."); | |
} |
النتيجة المخرجة:
A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A2 is Numeric Value: True
Cell A2 contains a DateTime value.
كيفية الحصول على قيمة تاريخ ووقت في Aspose.Cells
يُعين هذا المثال قيمة DateTime في الخلية A1 و A2، يضبط تنسيق مخصص ل A1 وتنسيق رقمي ل A2، يتحقق من أنواع القيمة في الخليتين، ثم يخرج قيمة DateTime وسلسلة مُنسَقة.
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."); | |
} |
النتيجة المخرجة:
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