Python - Convert excel serial date to datetime
Last Updated :
14 Sep, 2021
This article will discuss the conversion of an excel serial date to DateTime in Python.
The Excel "serial date" format is actually the number of days since 1900-01-00 i.e., January 1st, 1900. For example, the excel serial date number 43831 represents January 1st, 2020, and after converting 43831 to a DateTime becomes 2020-01-01.
By using xlrd.xldate_as_datetime() function this can be achieved. The xlrd.xldate_as_datetime() function is used to convert excel date/time number to datetime.datetime object.
Syntax: xldate_as_datetime (xldate, datemode)
Parameters: This function accepts two parameters that are illustrated below:
- xldate: This is the specified excel date that will converted into datetime.
- datemode: This is the specified datemode in which conversion will be performed.
Return values: This function returns the datetime.datetime object.
First, call xlrd.xldate_as_datetime(date, 0) function to convert the specified Excel date to a datetime.datetime object. Then, call datetime.datetime.date() function on the returned datetime.datetime object to return the date as a datetime.date object. Lastly, call datetime.date.isoformat() function to convert the returned datetime.date object to a ISO format date string.
Let's see some examples to illustrate the above algorithm:
Example: Python program to convert excel serial date to string date
Python3
# Python3 code to illustrate the conversion
# of excel serial date to datetime
# Importing xlrd module
import xlrd
# Initializing an excel serial date
xl_date = 43831
# Calling the xldate_as_datetime() function to
# convert the specified excel serial date into
# datetime.datetime object
datetime_date = xlrd.xldate_as_datetime(xl_date, 0)
# Calling the datetime_date.date() function to convert
# the above returned datetime.datetime object into
# datetime.date object
date_object = datetime_date.date()
# Calling the isoformat() function to convert the
# above returned datetime.date object into the
# ISO format date string
string_date = date_object.isoformat()
# Getting the converted date string as output
print(string_date)
# Getting the type of returned date format
print(type(string_date))
Output:
2020-01-01
<class 'str'>
Example 2: Python program to convert excel serial number to DateTime
Python3
# Python3 code to illustrate the conversion
# of excel serial date to datetime
# Importing xlrd module
import xlrd
# Initializing an excel serial date
xl_date = 43831
# Calling the xldate_as_datetime() function to
# convert the specified excel serial date into
# datetime.datetime object
datetime_date = xlrd.xldate_as_datetime(xl_date, 0)
# Calling the datetime_date.date() function to convert
# the above returned datetime.datetime object into
# datetime.date object
date_object = datetime_date.date()
# Getting the converted date date as output
print(date_object)
# Getting the type of returned date format
print(type(date_object))
Output:
2020-01-01
<class 'datetime.date'>
Similar Reads
Convert Date To Datetime In Python When you're programming, dealing with dates and times is important, and Python provides tools to manage them well. This article is about changing dates into date times in Python. We'll explore methods that can help you switch between these two types of data. Whether you're building a website or work
3 min read
How to convert a Python datetime.datetime to excel serial date number This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more
3 min read
How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read
Convert Python datetime to epoch Epoch time is a way to represent time as the number of seconds that have passed since January 1, 1970, 00:00:00 UTC. It is also known as Unix time or POSIX time and it serves as a universal point of reference for representing dates and times. Its used in various applications like file timestamps, da
2 min read
Pandas Convert Date (Datetime) To String Format We are given a column containing datetime values in a pandas DataFrame and our task is to convert these datetime objects into string format. This conversion is useful for visualizing data, exporting it to external applications, or performing string operations. For example, if we have this input:a =
4 min read