简体   繁体   中英

Convert Datetime in Epoch-Python

I want to convert a datetime into epoch.

For example, I have datetime in this format: 2018-02-14T22:15:00.232029+01:00 .

How can I convert it into epoch? I get a lot of errors when i try to use datetime or timestamp in python,because of my datetime's format. Any ideas?

This is one way:

import pandas as pd

base = '1970-01-01'
x = '2018-02-14T22:15:00.232029+01:00'

(pd.to_datetime(x) - pd.to_datetime(base)).total_seconds()

# 1518642900.232029

Without using any external libraries:

>>> import datetime
>>> datetime.datetime.now().strftime('%s.%f')
'1518655350.410295'

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