python 中的时间戳(年、月、日、小时、分钟)

Timestamp in python (year,month,day,hour,minute)

我目前正在使用

datetime.now()

我得到的结果例如

datetime.datetime(2020, 2, 4, 10, 14, 20, 768814)

我的目标是

datetime.datetime(2020, 2, 4, 10, 14, 20)

您可以使用 datetime.replace():

>>> datetime.now().replace(microsecond=0)
datetime.datetime(2020, 2, 4, 9, 24, 32)

很简单。如果您不想要微秒,请使用以下内容:

datetime.now().replace(microsecond=0)

Output ---> datetime.datetime(2020, 2, 4, 14, 53, 44)