1

the code below is just a basic subtract today's date from Halloween and xmas to get the difference in days, hours, minutes and seconds, I want to get rid of milliseconds. What is the best way to do that. I understand formatting dates with an f-string but not sure how to get rid of the millisecond results

import datetime as dt


halloween_date = dt.datetime(2020, 10, 31)
xmas_date = dt.datetime(2020, 12, 25)
today_date = dt.datetime.today()
time_between_halloween = today_date - halloween_date
time_between_xmas = today_date - xmas_date
print(f"""it is {time_between_halloween} until Halloween and \
{time_between_xmas} until christmas""")

he result includes a answer that looks like -74 days, 16:32:36.458040 I want to get rid of the millisecond? Update: I was able to get exactly what I wanted from the code posted below, it just seems that there should be a more eloquent way of getting the same results possibly with an imported module.

import datetime as dt


halloween_date = dt.datetime(2020, 10, 31)
xmas_date = dt.datetime(2020, 12, 25)
today_date = dt.datetime.today()
time_between_halloween = halloween_date - today_date
time_between_xmas = xmas_date - today_date
xmas_total_seconds = time_between_xmas.total_seconds()
xmas_total_minutes = xmas_total_seconds // 60
xmas_seconds = abs(xmas_total_seconds)
xmas_days = time_between_xmas.days
xmas_hours1 = xmas_seconds // 3600
xmas_hours = xmas_hours1 - (xmas_days * 24)
xmas_total_hour = (xmas_days * 24) + xmas_hours
xmas_minutes1 = xmas_seconds // 60
xmas_minutes = xmas_minutes1 - (xmas_total_hour * 60)
print(f"""Xmas is {xmas_days} days, {xmas_hours:.0f} hours and \
{xmas_minutes:.0f} minutes away.""")
hallo_total_seconds = time_between_halloween.total_seconds()
hallo_total_minutes = hallo_total_seconds // 60
hallo_seconds = abs(hallo_total_seconds)
hallo_days = time_between_halloween.days
hallo_hours1 = hallo_seconds // 3600
hallo_hours = hallo_hours1 - (hallo_days * 24)
hallo_total_hour = (hallo_days * 24) + hallo_hours
hallo_minutes1 = hallo_seconds // 60
hallo_minutes = hallo_minutes1 - (hallo_total_hour * 60)
print(f"""Halloween is {hallo_days} days, {hallo_hours:.0f} hours and \
{hallo_minutes:.0f} minutes away.""")

the output on this for my current time of 10:27 est 8/19 is below: xmas is 127 days, 13 hours and 32 minutes away. Halloween is 72 days, 13 hours and 32 minutes away.

I do see that I could make time_between_halloween and time_between_xmas =abs(xxx) and that would cut out a couple of lines, but it still seems long winded..

1 Answers1

1

datetime.timedelta objects hold their data in three time increments: days, seconds, microseconds.

The objects have the ability to natively expose any OR all three of these values, using attributes such as time_between_xmas.days OR time_between_xmas.microseconds.

An approach is to do this create a second timedelta object that has the same number of microseconds and subtract that number of microseconds from your object:

import datetime as dt

halloween_date = dt.datetime(2020, 10, 31)
xmas_date = dt.datetime(2020, 12, 25)
today_date = dt.datetime.today()
time_between_halloween = today_date - halloween_date
time_between_xmas = today_date - xmas_date


# Each of the following lines sets the number of microseconds
#     in the second date time object to be able to remove
#     that number of microseconds from your timedelta objects.
time_between_xmas = time_between_xmas - dt.timedelta(microseconds=time_between_xmas.microseconds)
time_between_halloween = time_between_halloween - dt.timedelta(microseconds=time_between_halloween.microseconds) 

print(f"""it is {time_between_halloween} until Halloween and \
{time_between_xmas} until christmas""")

 
E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
  • that worked and did exactly what I wanted it to do, thank you. I know in other situations with datetime there are ways to separate it into hours, minutes seconds so that this for example would print -129 days, 18 hours, 20 minutes and 16 with an object for each expression of time and then and f string to format..Thank you for your answer as this is exactly what I was asking and gave me some great insight as to a workaround for the exact question that I had...thank you – Victor Franchetti Aug 18 '20 at 22:23
  • @VictorFranchetti thanks for these kind words. If you feel like my response answered your question adequately, can you please go click on the Check Mark button located next to the answer? Much appreciated. – E. Ducateme Aug 19 '20 at 13:00
  • if you get a chance check out my edit to my post as I was able to get exactly what I wanted, but it just seems that there should be a more eloquent way of getting the same results than the way that I did it which was to take the total seconds and then do the math and place that in separate objects...thanks – Victor Franchetti Aug 19 '20 at 14:30