Search code examples
pythontimedata-analysis

How can I convert a time that is a number of seconds from a date to the actual date? [Python]


I have a list of that contains times. Each time is the number of seconds from Jan 1, 1904. This is not Unix or any other conversion I have familiar with. How can I convert this to the date?

An example of this is 3438012868.0 which is 3438012868.0 seconds from Jan 1, 1904. I want this date to be Dec 11, 2012, 1:00.30 pm


Solution

  • Use the datetime and timedelta classes for this kind of thing.

    from datetime import datetime, timedelta
    
    date = datetime(1904,1,1) # January 1st, 1904 at midnight
    
    delta = timedelta(seconds = 10000000)
    
    newdate = date + delta
    

    newdate will evaulate to datetime.datetime(1904, 4, 26, 17, 46, 40), which is April 26, 1904,

    Docs on these classes: https://docs.python.org/3/library/datetime.html