Convert "number" of excel date to python datetime

When I read an Excel file with python's xlrd etc., the date displayed on Excel is replaced with a 5-digit mysterious number such as ** 41496 **.

This seems to be because in Excel, the date starts from January 1, 1900 (the first day), and the date data is held as a numerical value obtained by adding the number of days from that.

--For example, 42884 on May 29, 2017. It seems that 42884 days have passed since January 1, 1900. -(By the way, if you put it in 1800 or Excel, it will not be converted numerically. It will be negative)

To convert this number to a date format in python, use timedelta and add it as follows: Substitute an Excel number for num

def excel_date(num):
    from datetime import datetime, timedelta
    return(datetime(1899, 12, 30) + timedelta(days=num))

For example, entering 41496 returns the datetime of August 10, 2013.

excel_date(41946)

> datetime.datetime(2013, 8, 10, 0, 0)

Recommended Posts

Convert "number" of excel date to python datetime
[python] Convert date to string
Function to convert Excel column to number
Conversion of string <-> date (date, datetime) in Python
Convert Excel data to JSON with python
Convert Python date types to RFC822 format
Convert data with shape (number of data, 1) to (number of data,) with numpy.
[Python] How to change character string (str) data to date (strptime of datetime)
Convert Windows epoch values to date with python
Convert json to excel
Batch conversion of Excel files to JSON [Python]
python beginners tried to predict the number of criminals
How to get the number of digits in Python
0 Convert unfilled date to datetime type with regular expression
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Summary of date processing in Python (datetime and dateutil)
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert python 3.x code to python 2.x
Python / datetime> Implementation to convert YYYYMMDD format to YYYY / MM / DD
Convert Excel file to text in Python for diff purposes
Convert markdown to PDF in Python
Workflow to convert formula (image) to python
Convert list to DataFrame with python
Python> list> Convert double list to single list
[Python] Convert natural numbers to ordinal numbers
How to calculate date with python
Convert decimal numbers to n-ary numbers [python]
Python> tuple> Convert double tuple to single tuple
Determine the date and time format in Python and convert to Unixtime
Made it possible to convert PNG to JPG with Pillow of Python
I tried to make a regular expression of "date" using Python
Convert a large number of PDF files to text files using pdfminer
Convert the result of python optparse to dict and utilize it
[Good By Excel] python script to generate sql to convert csv to table
[Python] How to convert db file to csv
[Python] Summary of how to use pandas
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
[Python] A program that calculates the number of socks to be paired
[Python] Convert csv file delimiters to tab delimiters
Note: [Python3] Convert datetime to a string in any format you like
Convert psd file to png in Python
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
[Python] How to put any number of standard inputs in a list
Convert Hiragana to Romaji with Python (Beta)
Convert from katakana to vowel kana [python]
Function to save images by date [python3]
I tried to open the latest data of the Excel file managed by date in the folder with Python
I want to batch convert the result of "string" .split () in Python
Convert FX 1-minute data to 5-minute data with Python
python> Convert tuple to list> aList = list (pi_tuple)
[Python2.7] Summary of how to use unittest
Try to operate Excel using Python (Xlwings)
Receive date type (datetime) with ArgumentParser [python]
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value