简体   繁体   中英

Convert Microsoft Timestamp to ISO 8601 Datetime format

Recently I've came across new timestamp format I haven't seen before, it called "Microsoft Timestamp" and it means days since Dec 31 1899. I've tried to search the web to see if there is any method in python able to convert this format to ISO 8601 and vice-versa, but didn't found anything useful.

ie according to https://www.silisoftware.com/tools/date.php?inputdate=43981.7155208333350&inputformat=microsoft 43981.715520833 refers to 2020-05-30T17:10:21+00:00

You could write your own function using the datetime module:

from datetime import datetime, timedelta

def microsoft_to_iso8601(microsoft):
    base_date = datetime(1899, 12, 31)
    ret_date = base_date + timedelta(days=microsoft)
    return ret_date.isoformat()

print(microsoft_to_iso8601(43981.7155208333350))
# returns 2020-05-31T17:10:21

The function above creates one datetime.datetime object, base_date , that stores the base date of the "Microsoft Timestamp": December 31st, 1899.

The ret_date is the first date plus the amount of days passed since that date represented by the "Microsoft Timestamp". The function returns this date converted into an ISO-8601 string.

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