كيفية إدارة التواريخ والأوقات
كيفية تخزين التواريخ والأوقات في إكسل
يتم تخزين التواريخ والأوقات في الخلايا على أنها أرقام. وبالتالي، قيم الخلايا التي تحتوي على تواريخ وأوقات تكون من نوع رقمي. الرقم الذي يحدد تاريخًا ووقتًا يتكون من مكونات التاريخ (الجزء الصحيح) والوقت (الجزء الكسري). يُرجع خاصية Cell.DoubleValue هذا الرقم.
كيفية عرض التواريخ والأوقات في Aspose.Cells
لعرض رقم كتاريخ ووقت، قم بتطبيق تنسيق التاريخ والوقت المطلوب على الخلية عبر الخاصية Style.Number أو 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. لاستخدام هذا النظام في جدول العمل، قم بضبط الخاصية Workbook.Settings.Date1904 إلى true.
تُظهر هذا المثال أن القيم المتسلسلة المخزنة في نفس التاريخ في أنظمة تاريخ مختلفة هي مختلفة.
//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); | |
} |
A1 is Numeric Value: 45253
use The 1904 date system====================
A2 is Numeric Value: 43791
كيفية تعيين قيمة تاريخ ووقت في Aspose.Cells
يُعين هذا المثال قيمة DateTime في الخلية A1 و A2، يضبط تنسيق مخصص ل A1 وتنسيق رقمي ل A2، ثم يخرج أنواع القيمة.
//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."); | |
} |
النتيجة المخرجة:
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 وسلسلة مُنسَقة.
//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."); | |
} |
النتيجة المخرجة:
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