Skip to content Skip to sidebar Skip to footer

Convert Date From String Format To Ole Automation Date

I have a date string 21-Apr-2018. How do I convert this date string into OLE automation date in python? I am using Python v3.6. Definition of OLE date can be found here. https://ms

Solution 1:

There are at least a couple of ways. You can calculate manually from OLE origin date, or use a 3rd party library such as xlrd.

In each case, you will need to convert your string to a datetime object.

from datetime import datetime
from xlrd import xldate

defdatetime2ole(date):
    date = datetime.strptime(date, '%d-%b-%Y')
    OLE_TIME_ZERO = datetime(1899, 12, 30)
    delta = date - OLE_TIME_ZERO
    returnfloat(delta.days) + (float(delta.seconds) / 86400)  # 86,400 seconds in daydefdatetime2xl(date):
    date = datetime.strptime(date, '%d-%b-%Y')
    parts = ('year', 'month', 'day', 'hour', 'minute', 'second')
    components = tuple(getattr(date, x) for x in parts)
    return xldate.xldate_from_datetime_tuple(components, 0)

print(datetime2ole('22-Apr-2018'))  # 43212.0print(datetime2xl('22-Apr-2018'))   # 43212.0

Post a Comment for "Convert Date From String Format To Ole Automation Date"