如何删除日期时间 python 中的毫秒数?
How to remove milliseconds in datetime python?
我想打印圣诞节前的天数、小时数、分钟数和秒数,但是当它打印时,它也给了我毫秒数。我试过 print(remain_until_xmas.strip(.)
但没有用。这是代码
import datetime as dt
xmas = dt.datetime(2020, 12, 25)
now = dt.datetime.now()
until_xmas = xmas - now
print(until_xmas)
要实时获取时间,您可以使用此代码:
import time
time = str(time.strftime("%M")) + ":" + str(time.strftime("%S"))
print(time)
这将为您提供以分秒为单位的当前时间,如果将其置于循环中,它将不断自我更新。
如果你想要日期和月份,你可以使用
print(time.strftime("%A, %B %e")) -
使用此代码,您可以从当前日期中减去圣诞节日期,并检索到您想要的内容。
这将是您的最终代码:
import time
month = time.strftime("%m")
day = time.strftime("%d")
hour = time.strftime("%H")
minute = time.strftime("%M")
second = time.strftime("%S")
chrmonth = 12
chrday = 23
chrhour = 12
chrminute = 60
chrsecond = 60
print("The time until christmas is, ", int(chrmonth) - int(month), "months", int(chrday) - int(day), "days", int(chrhour) - int(hour), "hours", int(chrminute) - int(minute), "minutes", int(chrsecond) - int(second), "seconds")
希望这对您有所帮助!
你可以使用.strftime()
print(until_xmas.strftime("%m/%d/%Y, %H:%M:%S"))
查看更多 here about strftime。
我想打印圣诞节前的天数、小时数、分钟数和秒数,但是当它打印时,它也给了我毫秒数。我试过 print(remain_until_xmas.strip(.)
但没有用。这是代码
import datetime as dt
xmas = dt.datetime(2020, 12, 25)
now = dt.datetime.now()
until_xmas = xmas - now
print(until_xmas)
要实时获取时间,您可以使用此代码:
import time
time = str(time.strftime("%M")) + ":" + str(time.strftime("%S"))
print(time)
这将为您提供以分秒为单位的当前时间,如果将其置于循环中,它将不断自我更新。
如果你想要日期和月份,你可以使用
print(time.strftime("%A, %B %e")) -
使用此代码,您可以从当前日期中减去圣诞节日期,并检索到您想要的内容。
这将是您的最终代码:
import time
month = time.strftime("%m")
day = time.strftime("%d")
hour = time.strftime("%H")
minute = time.strftime("%M")
second = time.strftime("%S")
chrmonth = 12
chrday = 23
chrhour = 12
chrminute = 60
chrsecond = 60
print("The time until christmas is, ", int(chrmonth) - int(month), "months", int(chrday) - int(day), "days", int(chrhour) - int(hour), "hours", int(chrminute) - int(minute), "minutes", int(chrsecond) - int(second), "seconds")
希望这对您有所帮助!
你可以使用.strftime()
print(until_xmas.strftime("%m/%d/%Y, %H:%M:%S"))
查看更多 here about strftime。