如何在Javascript上使用新的Date()。toUTCString()获取python结果?
在我做的Javascript上:
new Date().toUTCString()
"Tue, 08 Sep 2015 09:45:32 GMT"
在Python上
import datetime # or time, or either
date = ??? # some code
print date # >>> "Tue, 08 Sep 2015 09:45:32 GMT"
答案 0 :(得分:4)
时间格式与RFC 2822 format (used in emails)类似:
>>> import email.utils
>>> email.utils.formatdate(usegmt=True)
'Tue, 08 Sep 2015 10:06:04 GMT'
或者您可以使用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'
答案 1 :(得分:0)
试试这个..
from datetime import datetime
date = datetime.utcnow().ctime()
print date # >>> 'Tue Sep 8 10:10:22 2015'
答案 2 :(得分:0)
import datetime
date = datetime.datetime.utcnow().strftime("%c")
print date # >>> 'Tue Sep 8 10:14:17 2015'`