Tarih ve Saatleri Nasıl Yönetilir

Tarih ve Saatleri Excel’de Saklama

Tarihler ve saatler, hücrelerde sayı olarak saklanır. Bu nedenle, tarih ve saat içeren hücrelerin değerleri sayı türündedir. Tarih ve saat belirten bir sayı, tarih (tamsayı kısmı) ve saat (kesirli kısmı) bileşenlerinden oluşur. Cell.DoubleValue özelliği bu sayıyı döndürür.

Tarihleri ve Saatleri Aspose.Cells’de Gösterme

Bir sayıyı tarih ve saat olarak göstermek için hücreye Style.Number veya Style.Custom özelliği aracılığıyla gerekli tarih ve saat biçimini uygulayın. CellValue.DateTimeValue özelliği, hücrede bulunan sayı ile temsil edilen tarih ve saati belirten DateTime nesnesini döndürür.

Aspose.Cells’te iki tarih sistemi arasında nasıl geçiş yapılır

MS-Excel, tarihleri seri değerler olarak adlandırılan sayılar olarak saklar. Bir seri değer, tarih sistemine göre ilk günden geçen günlerin sayısıdır. Excel, seri değerler için aşağıdaki tarih sistemlerini destekler:

  1. 1900 tarih sistemi. İlk tarih 1 Ocak 1900’dür ve seri değeri 1’dir. Son tarih 31 Aralık 9999’dur ve seri değeri 2.958.465’tir. Bu tarih sistemi, ön tanımlı olarak çalışbook’ta kullanılır.
  2. 1904 tarih sistemi. İlk tarih 1 Ocak 1904’tür ve seri değeri 0’dır. Son tarih 31 Aralık 9999’dur ve seri değeri 2.957.003’tür. Çalışbook’ta bu tarih sistemi kullanılacaksa, Workbook.Settings.Date1904 özelliğini true olarak ayarlayın.

Bu örnek, farklı tarih sistemlerinde aynı tarihte saklanan seri değerlerin farklı olduğunu göstermektedir.

from aspose.cells import CellValueType, Workbook
from datetime import datetime
# Instantiating an Workbook object
workbook = Workbook()
workbook.settings.date1904 = False
# Obtaining the reference of the newly added worksheet
ws = workbook.worksheets[0]
cells = ws.cells
dateData = datetime(2023, 11, 23)
# Setting the DateTime value to the cells
a1 = cells.get("A1")
a1.put_value(dateData)
# Check if the cell contains a numeric value
if a1.type == CellValueType.IS_NUMERIC:
print("A1 is Numeric Value: " + str(a1.double_value))
workbook.settings.date1904 = True
print("use The 1904 date system====================")
# Setting the DateTime value to the cells
a2 = cells.get("A2")
a2.value = dateData
# Check if the cell contains a numeric value
if a2.type == CellValueType.IS_NUMERIC:
print("A2 is Numeric Value: " + str(a2.double_value))
Çıktı sonucu:

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

Aspose.Cells’te DateTime Değerini Nasıl Ayarlar

Bu örnek, A1 ve A2 hücresine bir DateTime değeri ayarlar, A1’in özel biçimini ve A2’nin sayı biçimini ayarlar ve ardından değer tiplerini çıktılar.

from aspose.cells import CellValueType, Workbook
from datetime import datetime
# Instantiating an Workbook object
workbook = Workbook()
# Obtaining the reference of the newly added worksheet
ws = workbook.worksheets[0]
cells = ws.cells
# Setting the DateTime value to the cells
a1 = cells.get("A1")
a1.put_value(datetime.now())
# Check if the cell contains a numeric value
if a1.type == CellValueType.IS_NUMERIC:
print("A1 is Numeric Value: " + str(a1.is_numeric_value))
a1Style = a1.get_style()
# Set custom Datetime style
a1Style.custom = "mm-dd-yy hh:mm:ss"
a1.set_style(a1Style)
# Check if the cell contains a DateTime value
if a1.type == CellValueType.IS_DATE_TIME:
print("Cell A1 contains a DateTime value.")
else:
print("Cell A1 does not contain a DateTime value.")
# Setting the DateTime value to the cells
a2 = cells.get("A2")
a2.value = datetime.now()
# Check if the cell contains a numeric value
if a2.type == CellValueType.IS_NUMERIC:
print("A2 is Numeric Value: " + str(a2.is_numeric_value))
a2Style = a2.get_style()
# Set the display format of numbers and dates.
a2Style.number = 22
a2.set_style(a2Style)
# Check if the cell contains a DateTime value
if a2.type == CellValueType.IS_DATE_TIME:
print("Cell A2 contains a DateTime value.")
else:
print("Cell A2 does not contain a DateTime value.")

Çıktı sonucu:

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

Aspose.Cells’te DateTime Değeri Nasıl Alınır

Bu örnek, A1 ve A2 hücresine bir DateTime değeri ayarlar, A1’in özel biçimini ve A2’nin sayı biçimini ayarlar, iki hücrenin değer tiplerini kontrol eder ve ardından DateTime değerini ve biçimlendirilmiş dizesini çıktılar.

from aspose.cells import CellValueType, Workbook
from datetime import datetime
# Instantiating an Workbook object
workbook = Workbook()
# Obtaining the reference of the newly added worksheet
ws = workbook.worksheets[0]
cells = ws.cells
# Setting the DateTime value to the cells
a1 = cells.get("A1")
a1.put_value(datetime.now())
# Check if the cell contains a numeric value
if a1.type == CellValueType.IS_NUMERIC:
print("A1 is Numeric Value: " + str(a1.is_numeric_value))
a1Style = a1.get_style()
# Set custom Datetime style
a1Style.custom = "mm-dd-yy hh:mm:ss"
a1.set_style(a1Style)
# Check if the cell contains a DateTime value
if a1.type == CellValueType.IS_DATE_TIME:
print("Cell A1 contains a DateTime value.")
# Get the DateTime value
dateTimeValue = a1.date_time_value
# Now, you can use dateTimeValue as needed
print("A1 DateTime Value: " + str(dateTimeValue))
# Output date formatted string
print("A1 DateTime String Value: " + a1.string_value)
else:
print("Cell A1 does not contain a DateTime value.")
# Setting the DateTime value to the cells
a2 = cells.get("A2")
a2.value = datetime.now()
# Check if the cell contains a numeric value
if a2.type == CellValueType.IS_NUMERIC:
print("A2 is Numeric Value: " + str(a2.is_numeric_value))
a2Style = a2.get_style()
# Set the display format of numbers and dates.
a2Style.number = 22
a2.set_style(a2Style)
# Check if the cell contains a DateTime value
if a2.type == CellValueType.IS_DATE_TIME:
print("Cell A2 contains a DateTime value.")
# Get the DateTime value
dateTimeValue = a2.date_time_value
# Now, you can use dateTimeValue as needed
print("A2 DateTime Value: " + str(dateTimeValue))
# Output date formatted string
print("A2 DateTime String Value: " + a2.string_value)
else:
print("Cell A2 does not contain a DateTime value.")

Çıktı sonucu:

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