Reputation: 504
How I can get on python result as new Date().toUTCString() on Javascript?
on Javascript I make:
new Date().toUTCString()
"Tue, 08 Sep 2015 09:45:32 GMT"
on Python
import datetime # or time, or either
date = ??? # some code
print date # >>> "Tue, 08 Sep 2015 09:45:32 GMT"
Upvotes: 3
Views: 1851
Reputation: 414215
The time format looks similar to RFC 2822 format (used in emails):
>>> import email.utils
>>> email.utils.formatdate(usegmt=True)
'Tue, 08 Sep 2015 10:06:04 GMT'
Or you can get any time format you want using datetime.strftime()
:
>>> from datetime import datetime, timezone
>>> d = datetime.now(timezone.utc)
>>> d
datetime.datetime(2015, 9, 8, 10, 6, 4, tzinfo=datetime.timezone.utc)
>>> str(d)
'2015-09-08 10:06:04+00:00'
>>> d.strftime('%a, %d %b %Y %H:%M:%S %Z')
'Tue, 08 Sep 2015 10:06:04 UTC'
>>> d.strftime('%a, %d %b %Y %H:%M:%S %z')
'Tue, 08 Sep 2015 10:06:04 +0000'
where timezone.utc
is defined here.
Upvotes: 6
Reputation: 1120
Try this..
from datetime import datetime
date = datetime.utcnow().ctime()
print date # >>> 'Tue Sep 8 10:10:22 2015'
Upvotes: 0
Reputation: 2719
import datetime
date = datetime.datetime.utcnow().strftime("%c")
print date # >>> 'Tue Sep 8 10:14:17 2015'`
Upvotes: 1