1

I dumps a datetime object into to json object and I get:

a = u'2015-02-17T15:06:23.869000'

How to transfer it back to a datetime object?

Cœur
  • 34,719
  • 24
  • 185
  • 251
jianfeng
  • 13
  • 1
  • 5

2 Answers2

2

You can use the following code:

>>> import datetime
>>> datetime.datetime.strptime(u"2015-02-17T15:06:23.869000", "%Y-%m-%dT%H:%M:%S.%f")
datetime.datetime(2015, 2, 17, 15, 6, 23, 869000)

to parse it back to a datetime object. JSON has no special representation for a datetime object, so your program encodes it as a string.

Unknown
  • 5,506
  • 4
  • 42
  • 63
1

One option is to let dateutil do the job:

>>> from dateutil import parser
>>> a = u'2015-02-17T15:06:23.869000' 
>>> parser.parse(a)
datetime.datetime(2015, 2, 17, 15, 6, 23, 869000)

you could also look for some reference here Convert unicode to datetime proper strptime format

Community
  • 1
  • 1
vdkotian
  • 539
  • 4
  • 13