Reputation: 1795
How can i convert this timestamp value to an integer representing seconds-since-the-epoch in python
2016-08-06T06:07:36.349Z
This is the timestamp value i received in elastic search query.
I tried searching but the timestamp format was different from this and this didn't helped neither any other
Upvotes: 1
Views: 6025
Reputation: 2280
You can use python inbuilt datetime package and its strptime method to convert string into datetime object.
from datetime import datetime
datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT%H:%M:%S.%fZ")
after that you should get epoch datetime object which you can get by
epoch = datetime.utcfromtimestamp(0)
your final seconds can be derived from this method
def unix_time_millis(datetime):
return (datetime - epoch).total_seconds() * 1000.0
so your complete code looks like
from datetime import datetime
epoch = datetime.utcfromtimestamp(0)
def unix_time_millis(datetime):
return (datetime - epoch).total_seconds() * 1000.0
current_date = datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT%H:%M:%S.%fZ")
print unix_time_millis(current_date)
This answer is inspired from this answer https://stackoverflow.com/a/11111177/4453633
Upvotes: 6