Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting date in specific format in python

Tags:

python

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")
like image 399
user308827 Avatar asked Jun 21 '26 11:06

user308827


1 Answers

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'
like image 191
TigerhawkT3 Avatar answered Jun 23 '26 01:06

TigerhawkT3