What would be the equivalent of time.ctime()
for UTC time?
什么相当于UTC时间的time.ctime()?
Currently, if I enter time.ctime()
in the interpreter, I get this:
目前,如果我在解释器中输入time.ctime(),我会得到:
'Mon Feb 5 17:24:48 2018'
This returns the local time and I would like to know how to get time in the same format except in UTC.
这将返回本地时间,我想知道如何以相同的格式获得时间,但UTC除外。
Edit: It's time.asctime(time.gmtime())
编辑:这是time.asctime(time.gmtime())
4 个解决方案
#1
4
time.gmtime()
returns an object representing the utctime.
time.gmtime()返回表示utctime的对象。
To get it into the same format as time.ctime
, you can use time.asctime()
.
要使其与time.ctime具有相同的格式,可以使用time.asctime()。
>>> time.asctime(time.gmtime())
'Mon Feb 5 12:03:39 2018'
#2
1
This should work:
这应该工作:
import datetime
utc = datetime.datetime.utcnow()
# datetime.datetime(2018, 2, 5, 12, 2, 56, 678723)
str(utc)
# '2018-02-05 12:05:10.617973'
#3
1
import datetime
print(datetime.datetime.utcnow())
2018-02-05 12:05:53.329809
2018-02-05 12:05:53.329809
#4
1
Below is the code where you can select any timezone:
以下是您可以选择任何时区的代码:
from datetime import datetime, timezone
utc_dt = datetime.now(timezone.utc)
#1
4
time.gmtime()
returns an object representing the utctime.
time.gmtime()返回表示utctime的对象。
To get it into the same format as time.ctime
, you can use time.asctime()
.
要使其与time.ctime具有相同的格式,可以使用time.asctime()。
>>> time.asctime(time.gmtime())
'Mon Feb 5 12:03:39 2018'
#2
1
This should work:
这应该工作:
import datetime
utc = datetime.datetime.utcnow()
# datetime.datetime(2018, 2, 5, 12, 2, 56, 678723)
str(utc)
# '2018-02-05 12:05:10.617973'
#3
1
import datetime
print(datetime.datetime.utcnow())
2018-02-05 12:05:53.329809
2018-02-05 12:05:53.329809
#4
1
Below is the code where you can select any timezone:
以下是您可以选择任何时区的代码:
from datetime import datetime, timezone
utc_dt = datetime.now(timezone.utc)