Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make Javascript new Date().toUTCString() into Python

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"
like image 631
Yuriy Leonov Avatar asked Oct 15 '25 18:10

Yuriy Leonov


2 Answers

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.

like image 154
jfs Avatar answered Oct 17 '25 08:10

jfs


    import datetime
    date = datetime.datetime.utcnow().strftime("%c")
    print date # >>> 'Tue Sep  8 10:14:17 2015'`
like image 30
zabusa Avatar answered Oct 17 '25 07:10

zabusa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!