I have string in next format: 2017-02-14T09:51:46.000-0600
What is the best approach to parse and convert string into timestamp? I have options to use regular expression or to write my own function for parsing, but are there any builtin methods which can help me?
First part would be creating datetime object:
from datetime import datetime
date_string = "2017-02-14T09:51:46.000-0600"
# I'm using date_string[:-9] to skip ".000-0600"
format_date = datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%f%z'))
After which format date is:
print(format_date)
2017-02-14 09:51:46
And timestamp is:
print(format_date.timestamp())
1487062306.0
Little clarification here, on python reference page, you can see definition for '%Y-%m-%dT%H:%M:%S.%f%z') format specifiers I used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With