如何将Python datetime.datetime 转换为 excel 序列日期号
本文将讨论Python datetime.datetime 到excel 序列日期数的转换。 Excel“序列日期”格式实际上是自 1900-01-00 以来的天数。这 strftime()函数用于将日期和时间对象转换为其字符串表示形式。它接受一个或多个格式化代码的输入并返回字符串表示。
Syntax:
strftime(format)
Parameters: This function accepts a parameter which is illustrated below:
- format: This is the specified format code in which the given date and time object is going to be represented.
Return values: It returns the string representation of the date or time object.
例1:在下面的例子中,当前日期和时间被转换成excel序列日期号。并且返回的输出将采用'08/23/21 15:15:53'的格式,它被Excel接受为有效的日期/时间并允许在Excel中进行排序。
Python3
# Python3 code to illustrate the conversion of
# datetime.datetime to excel serial date number
# Importing datetime module
import datetime
# Calling the now() function to return
# current date and time
current_datetime = datetime.datetime.now()
# Calling the strftime() function to convert
# the above current datetime into excel serial date number
print(current_datetime.strftime('%x %X'))
Python3
# Python3 code to illustrate the conversion of
# datetime.datetime to excel serial date number
# Importing date module from datetime
from datetime import date
# Taking the parameter from the calling function
def convert_date_to_excel_ordinal(day, month, year):
# Specifying offset value i.e.,
# the date value for the date of 1900-01-00
offset = 693594
current = date(year, month, day)
# Calling the toordinal() function to get
# the excel serial date number in the form
# of date values
n = current.toordinal()
return (n - offset)
# Calculating the excel serial date number
# for the date "02-02-2021" by calling the
# user defined function convert_date_to_excel_ordinal()
print(convert_date_to_excel_ordinal(2, 2, 2021))
Python3
# Python3 code to illustrate the conversion of
# datetime.datetime to excel serial date number
# Importing datetime module
from datetime import datetime
import datetime as dt
def excel_date(date1):
# Initializing a reference date
# Note that here date is not 31st Dec but 30th!
temp = dt.datetime(1899, 12, 30)
delta = date1 - temp
return float(delta.days) + (float(delta.seconds) / 86400)
# Calculating the excel serial date number
# for the date "2021-05-04" by calling the
# user defined function excel_date()
print(excel_date(datetime(2021, 2, 4)))
输出:
08/23/21 15:15:53
如果我们需要日期值形式的 excel 序列日期编号,那么可以使用toordinal()函数来完成。
示例 2:日期值形式的序列号
蟒蛇3
# Python3 code to illustrate the conversion of
# datetime.datetime to excel serial date number
# Importing date module from datetime
from datetime import date
# Taking the parameter from the calling function
def convert_date_to_excel_ordinal(day, month, year):
# Specifying offset value i.e.,
# the date value for the date of 1900-01-00
offset = 693594
current = date(year, month, day)
# Calling the toordinal() function to get
# the excel serial date number in the form
# of date values
n = current.toordinal()
return (n - offset)
# Calculating the excel serial date number
# for the date "02-02-2021" by calling the
# user defined function convert_date_to_excel_ordinal()
print(convert_date_to_excel_ordinal(2, 2, 2021))
输出:
44229
示例:在下面的示例中,参考 1899-12-30 日期将“2021-05-04”日期转换为 excel 序列日期编号。
蟒蛇3
# Python3 code to illustrate the conversion of
# datetime.datetime to excel serial date number
# Importing datetime module
from datetime import datetime
import datetime as dt
def excel_date(date1):
# Initializing a reference date
# Note that here date is not 31st Dec but 30th!
temp = dt.datetime(1899, 12, 30)
delta = date1 - temp
return float(delta.days) + (float(delta.seconds) / 86400)
# Calculating the excel serial date number
# for the date "2021-05-04" by calling the
# user defined function excel_date()
print(excel_date(datetime(2021, 2, 4)))
输出:
44231.0