2

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??

Rushabh Sudame
  • 414
  • 1
  • 6
  • 22

2 Answers2

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
  • 7,268
  • 6
  • 44
  • 58
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
  • 43,482
  • 10
  • 63
  • 98