I have string like this 2019-06-13 23:37:02.284175.
I would like to convert this string to unix time epoch.
How can I convert this string to unix timestamp using python??
Asked
Active
Viewed 314 times
2
Rushabh Sudame
- 374
- 4
- 20
2 Answers
2
from datetime import datetime
string_date = '2019-06-13 23:37:02.284175'
date_format = '%Y-%m-%d %H:%M:%S.%f'
epoch_time = datetime(1970, 1, 1)
print((datetime.strptime(string_date, date_format) - epoch_time).total_seconds())
# 1560469022.284175
Kushan Gunasekera
- 3,907
- 2
- 31
- 40
2
In Python 3.7+ you can do this using datetime.datetime.fromisoformat:
import datetime
print(datetime.datetime.fromisoformat("2019-06-13 23:37:02.284175").timestamp())
Output:
1560469022.284175
ForceBru
- 41,233
- 10
- 61
- 89