0

I have a string date as 2015-03-25T00:00:00Z. How do I convert it to a unix epoch1426636800000.0

Are there any libraries in python to do that.

user2890683
  • 403
  • 2
  • 6
  • 18
  • This looks like an ISO date and has been answered [here](http://stackoverflow.com/questions/127803/how-to-parse-iso-formatted-date-in-python) – Harold Ship Mar 25 '15 at 11:01
  • Also, you can see some in http://stackoverflow.com/questions/11743019/convert-python-datetime-to-epoch-with-strftime. – Mihai8 Mar 25 '15 at 11:02

4 Answers4

0

time.strptime(string[, format])

import time

print time.strptime("2015-03-25T00:00:00Z","%Y-%m-%dT%H:%M:%SZ")
ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

Using time, for example.

So first you need to convert the string to time object (or you can use datetime alternatively as halex mentioned) and then get the seconds since epoch.

>>> import time
>>> time.mktime(time.strptime('2015-03-25T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ'))
1427241600.0
Hugo Sousa
  • 906
  • 2
  • 9
  • 27
0

If you have Python 3.3 or newer you can use the datetime module:

>>> import datetime
>>> datetime.datetime.strptime("2015-03-25T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ").timestamp() 
1427238000.0
halex
  • 16,253
  • 5
  • 58
  • 67
  • AttributeError: 'datetime.datetime' object has no attribute 'timestamp' is the error getting thrown – user2890683 Mar 25 '15 at 11:14
  • @user2890683 Oh. I forgot to mention that [`timestamp`](https://docs.python.org/3.4/library/datetime.html#datetime.datetime.timestamp) was introduced in Python 3.3. Seems like you are using Python2. Sorry for the inconvenience. – halex Mar 25 '15 at 11:15
0

You can use easy_date to make it easy:

import date_converter
timestamp = date_converter.string_to_timestamp("2015-03-25T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ")
Raphael Amoedo
  • 4,233
  • 4
  • 28
  • 37