如何获取当前时间?


当前回答

这应该行得通

import time

now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("The current time is", current_time)

其他回答

这应该行得通

import time

now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("The current time is", current_time)

以下是我用来获取时间而不必格式化的内容。有些人不喜欢拆分方法,但它在这里很有用:

from time import ctime
print ctime().split()[3]

它将以HH:MM:SS格式打印。

使用time.strftime():

>>> from time import gmtime, strftime
>>> strftime("%Y-%m-%d %H:%M:%S", gmtime())
'2009-01-05 22:14:39'
import datetime

print('date='+datetime.datetime.now().__str__().split(' ')[0]+' '+'time='+datetime.datetime.now().__str__().split(' ')[1]

由于Qt被广泛使用,

from PyQt5 import QDateTime
print(QDateTime.currentDateTime().__str__().split('(')[1].rstrip(')'))

获取当前时间并将其转换为字符串:

from datetime import datetime
datetime.now().strftime('%Y-%m-%d %H:%M:%S')