Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a given ordinal number (from Excel) to a date

I have a Value 38142 I need to convert it into date format using python. if use this number in excel and do a right click and format cell at that time the value will be converted to 04/06/2004 and I need the same result using python. How can I achieve this

like image 608
sudeep Krishnan Avatar asked Apr 01 '15 09:04

sudeep Krishnan


People also ask

How do I convert a number into a date in Excel?

Select the cells that have the number that you want to convert into a date. Click the 'Home' tab. In the 'Number' group, click on the Number Formatting drop-down. In the drop-down, select 'Long Date' or Short Date' option (based on what format would you want these numbers to be in)

How do you find ordinal dates in Excel?

Select a cell that used to place the ordinal date, click Kutools > Formula Helper > Date & Time > Convert date to ordinal date.

How do I change a 5 digit number to a date in Excel?

Select a blank cell (says cell B2) adjacent to the serial number cell you need to convert to date, then enter formula =TEXT(A2,"m/d/yyyy") into the Formula Bar, and press the Enter key. 2. Keep selecting cell B2, then drag its Fill Handle to the cell with the serial number you need to convert to date.


2 Answers

The offset in Excel is the number of days since 1900/01/01, with 1 being the first of January 1900, so add the number of days as a timedelta to 1899/12/31:

from datetime import datetime, timedelta  def from_excel_ordinal(ordinal, _epoch0=datetime(1899, 12, 31)):     if ordinal >= 60:         ordinal -= 1  # Excel leap year bug, 1900 is not a leap year!     return (_epoch0 + timedelta(days=ordinal)).replace(microsecond=0) 

You have to adjust the ordinal by one day for any date after 1900/02/28; Excel has inherited a leap year bug from Lotus 1-2-3 and treats 1900 as a leap year. The code above returns datetime(1900, 2, 28, 0, 0) for both 59 and 60 to correct for this, with fractional values in the range [59.0 - 61.0) all being a time between 00:00:00.0 and 23:59:59.999999 on that day.

The above also supports serials with a fraction to represent time, but since Excel doesn't support microseconds those are dropped.

like image 97
Martijn Pieters Avatar answered Sep 17 '22 21:09

Martijn Pieters


from datetime import datetime, timedelta  def from_excel_ordinal(ordinal, epoch=datetime(1900, 1, 1)):     # Adapted from above, thanks to @Martijn Pieters       if ordinal > 59:         ordinal -= 1  # Excel leap year bug, 1900 is not a leap year!     inDays = int(ordinal)     frac = ordinal - inDays     inSecs = int(round(frac * 86400.0))      return epoch + timedelta(days=inDays - 1, seconds=inSecs) # epoch is day 1  excelDT = 42548.75001           # Float representation of 27/06/2016  6:00:01 PM in Excel format   pyDT = from_excel_ordinal(excelDT) 

The above answer is fine for just a date value, but here I extend the above solution to include time and return a datetime values as well.

like image 21
David Avatar answered Sep 19 '22 21:09

David