-1

How to remove 0:00:00 from the output of datetime.date() function? For example:

import datetime
now = datetime.datetime.now()
year1 = now.strftime("%Y")
month2 = now.strftime("%m")
day3 = now.strftime("%d")
year = int(year1)
month = int(month2)
day = int(day3)
first_day = datetime.date(2021,8,1)
second_day = datetime.date(year,month,day)
daysleft = first_day - second_day
print(daysleft)

I get the output:

9 days, 0:00:00 

If you didn't understand the question title, My main goal is to remove the 0:00:00 before the period (.). I've seen many other questions like this (in stack overflow/exchange and other websites), but it was nothing in python coding language.

imxitiz
  • 3,920
  • 3
  • 9
  • 33

3 Answers3

1

You can get just the days by asking for the .days attribute of a datetime.timedelta object. E.g.:

print('{} days'.format(daysleft.days))
larsks
  • 277,717
  • 41
  • 399
  • 399
0

If your only goal is printing the output and you don't care about maintaining daysleft as a datetime object, you can always convert it to a string and .split() it like this:

print(str(daysleft).split(",")[0])
not_speshal
  • 22,093
  • 2
  • 15
  • 30
Jack
  • 1
  • 1
  • actually i need for printing in tkinter if curious –  Jul 24 '21 at 02:58
  • using in tkinter so i think it should be daysleft2 = str(daysleft).split(",")[0])| lbl = tk.Label(text=daysleft2)| lbl.pack()| the character | is for separating lines in the code –  Jul 24 '21 at 03:03
  • Yup, that looks good. My code above is only printing the days left as you intended, but the way you're storing it into a new variable should work fine. If you don't need the original string (daysleft) you can always reassign it like `daysleft = str((str(daysleft).split(",")[0])` so you don't have any extra vars. – Jack Jul 24 '21 at 17:44
0

datetime has many useful functions and values

To get only days you can daysleft.days

print(f'{daysleft.days} days')

but you can also reduce other code to

now = datetime.datetime.now()
daysleft = first_day - now.date()

or

today = datetime.date.today()
daysleft = first_day - today

BTW:

If you need year, month, day as numbers then you can get it as

year   = now.year
month  = now.month
day    = now.day
hour   = now.hour
minute = now.minute
second = now.second
ms     = now.microsecond
weekday = now.weekday()    # 0 = monday, ..., 6 = sunday
weekday = now.isoweekday() # 1 = monday, ..., 7 = sunday

Other useful function timedelta

one_day = datetime.timedelta(days=1)

today = datetime.date.today()

yesterday = today - one_day
tomorrow  = today + one_day

day_after_tomorrow = today + 2*one_day

Minimal working code with tkinter

import datetime

first_day = datetime.date(2021, 8, 1)

now = datetime.datetime.now()
daysleft = first_day - now.date()

today = datetime.date.today()
daysleft = first_day - today

print(f'{daysleft.days} days')

# ---

import tkinter as tk

root = tk.Tk()
lbl = tk.Label(root, text=f'{daysleft.days} days till {first_day}')
lbl.pack()
root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148