2

How can I get a date in the foll. format in python:

2016-04-26T19:50:48Z

I am doing this:

import datetime

now = datetime.now()
now.strftime("%Y %m %d %H:%M")

2 Answers 2

6

Well, first, you're not getting now() properly. That's in datetime.datetime, not in the top level datetime. Second, it doesn't seem like you've attempted to get the format string you wanted - it doesn't even have the dashes you specify.

>>> import datetime
>>> now = datetime.now()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'now'
>>> now = datetime.datetime.now()
>>> now.strftime("%Y %m %d %H:%M")
'2016 04 28 17:20'
>>> now.strftime("%Y-%m-%dT%H:%M:%SZ")
'2016-04-28T17:20:09Z'
Sign up to request clarification or add additional context in comments.

Comments

6

Well, it looks like you are trying to get the string representation of your date object in ISO 8601 format. I don't know if you really need Z at the end, here is an alternative way to .strftime():

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.replace(microsecond=0).isoformat()
'2016-04-28T17:29:53'

I am sure you can figure sth out to append Z to the string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.