我有以下日期时间:
dt = datetime.datetime(2021, 10, 15, 0, 0, tzinfo=datetime.timezone.utc)
我如何添加秒和微发送到它与python代码?它的结构和
一样datetime.datetime(2021, 10, 18, 15, 31, 21, 436248, tzinfo=datetime.timezone.utc)
可以使用timedelta。例如
from datetime import timedelta
dt = datetime.datetime(2021, 10, 15, 0, 0, tzinfo=datetime.timezone.utc)
if not dt.second:
dt = dt + timedelta(seconds=21)
if not dt.microsecond:
dt = dt + timedelta(microseconds=23)
我不太确定你想要做什么但是你可以这样做,如果你想要得到时间的话,使用datetime:
from datetime import datetime
#get seconds/microseconds
now = datetime.now()
seconds = int(now.strftime("%S"))
microseconds = int(now.strftime("%f"))
datetime.datetime(2021, 10, 15, 31, seconds, microseconds, tzinfo=datetime.timezone.utc)