0

I want to print the amount of days, hours, minutes and seconds until christmas day but when it prints, it also gives me the millisecond. I tried doing print(remain_until_xmas.strip(.) but that didn't work. Here is the code

import datetime as dt

xmas = dt.datetime(2020, 12, 25)
now = dt.datetime.now()
until_xmas = xmas - now

print(until_xmas)
AyeAreEm
  • 353
  • 6
  • 14

2 Answers2

1

To get the time live, you can use this code:

import time
time = str(time.strftime("%M")) + ":" + str(time.strftime("%S"))
print(time)

This will give you the current time in Minutes and Seconds, and if you put it in a loop, it will keep updating itself.

If you want the day and month, you can use

print(time.strftime("%A, %B %e")) - 

With this code, you can then subtract the Xmas date from the current date, and retrieve what you want.

This would be your final code:

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")

Hopefully this helps!

10 Rep
  • 2,217
  • 7
  • 19
  • 33
-1

you can use .strftime()

print(until_xmas.strftime("%m/%d/%Y, %H:%M:%S"))

see more here about strftime.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
SothanaV
  • 1
  • 1