Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Now to convert this strings to date time object in Python or django?

Tags:

python

django

Now to convert this strings to date time object in Python or django?

2010-08-17T19:00:00Z
2010-08-17T18:30:00Z
2010-08-17T17:05:00Z
2010-08-17T14:30:00Z
2010-08-10T22:20:00Z
2010-08-10T21:20:00Z
2010-08-10T20:25:00Z
2010-08-10T19:30:00Z
2010-08-10T19:00:00Z
2010-08-10T18:30:00Z
2010-08-10T17:30:00Z
2010-08-10T17:05:00Z
2010-08-10T17:05:00Z
2010-08-10T15:30:00Z
2010-08-10T14:30:00Z

whrn i do this datestr=datetime.strptime( datetime, "%Y-%m-%dT%H:%M:%S" )

it tell me that unconverted data remains: Z

like image 959
Pol Avatar asked Oct 12 '10 20:10

Pol


People also ask

How do I convert string to datetime in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.


2 Answers

You can parse the strings as-is without the need to slice if you don't mind using the handy dateutil module. For e.g.

>>> from dateutil.parser import parse
>>> s = "2010-08-17T19:00:00Z"
>>> parse(s)
datetime.datetime(2010, 8, 17, 19, 0, tzinfo=tzutc())
>>> 
like image 131
Manoj Govindan Avatar answered Nov 10 '22 11:11

Manoj Govindan


Use slicing to remove "Z" before supplying the string for conversion

datestr=datetime.strptime( datetime[:-1], "%Y-%m-%dT%H:%M:%S" )

>>> test = "2010-08-17T19:00:00Z"
>>> test[:-1]
'2010-08-17T19:00:00'
like image 25
pyfunc Avatar answered Nov 10 '22 09:11

pyfunc