简体   繁体   中英

Epoch time python and excel

I am trying to automate some data processing on an excel file and in the data processing file I use a macro that I guess converts the date column from "mm/dd/yyyy" into the days since January 1, 1900 as per excel convention. So when I try to do a similar thing in Python 2.7.4. It seems to be 2 days off. For example, 2/25/2015 becomes 42060 when done by the excel macro. When I run this code:

import datetime

gotDay = datetime.date(2015,2,25)
epoch = datetime.date(1900,1,1)

print((gotDay-epoch).days)

I get 42058.

I could totally just add 2 to everything in Python, but I was wondering why this happens.

Thanks!

The correct answer is 42058, so there's something wrong w/the excel macro.

My guess is that excel is counting both the start and end date as a day, when really you're only interested in the time between those dates.

It's like saying how many days between today and tomorrow. You could say one, which is what python is doing, or you could say 3 (today, the day between, and tomorrow) which is what I'm guessing your excel macro is doing.

This is an old thread, but since it came up in google, I wanted to put in an answer. The off-by-two problem is explained like this:

  1. One day because 1900-01-01 is day #1, not day #0. In Excel, if you convert 1900/1/1 into a number, it gives the answer 1, not 0. In python: print((datetime.date(1900,1,1)-datetime.date(1900,1,1)).days) = 0.

  2. Another day because Excel implemented a bug in Lotus 1-2-3 for backwards compatibility. Apparently, Lotus 1-2-3 incorrectly assumed that 1900 was a leap year. https://support.microsoft.com/en-us/help/214326/excel-incorrectly-assumes-that-the-year-1900-is-a-leap-year

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM