Home python python how many days left, min, sec from date to date

python how many days left, min, sec from date to date




I have two dates: current and new year.

It is clear that through the DateTime in Python 3 you can deduct these dates and get a new one, but how can you calculate the amount of time to the new year in seconds, days, minutes , hours, etc.


to the new year: 60 days 22 hours 45 minutes 12 seconds.

Answer 1, Authority 100%

Why not see what happens if subtract these dates .
And it turns out the object class Timedelta, which contains what you need

now = datetime.datetime.today ()
NY = DateTime.dateTime (2020,1,1)
D = NY-NOW # STR (D) '83 Days, 2: 43: 10.517807 '
MM, SS = DIVMOD (D.Seconds, 60)
HH, MM = Divmod (mm, 60)
Print ('To New Year: {} days {} hour {} min {} sec.'. Format (D.days, HH, MM, SS))
# New Year: 83 days 2 hours 43 min 10 seconds.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions