【问题标题】:Find the difference between the chosen date and current date in python (in Days)在python中查找所选日期和当前日期之间的差异(以天为单位)
【发布时间】:2021-08-30 19:11:51
【问题描述】:

我想从 tkcalendar 中选择日期并找出差异 在所选日期和当前日期之间。有人可以帮忙吗? (作为 尽可能简单)另外,我尝试安装了 tkcalendar,我可以 使用它,但 vscode 说 report missing import

newToProgramming无法从其他人那里弄清楚

from tkinter import *
try :
    from tkcalendar import *
except:
    pass

root = Tk()  # Creating instance of Tk class
root.title("Centering windows")
root.resizable(False, False)  # This code helps to disable windows from resizing

root.geometry("200x200+600+100")

def days():
    # find the difference between the current date and the choosen date
    pass

Label(root, text= 'Pick Up Date :').pack()
txt_pdate = DateEntry(root)
txt_pdate.pack()

txt_pdate.get_date()

btn = Button(root, text='click', command= days).pack()


root.mainloop()

【问题讨论】:

    标签: python date tkcalendar


    【解决方案1】:

    只需安装 python-dateutil

    
    pip3 install python-dateutil # or pip install python-dateutil
    
    

    然后:

    
    ...
    
    from dateutil.relativedelta import relativedelta
    from datetime import datetime as dt
    ...
    
    difference = relativedelta(txt_pdate.get_date(), dt.today())
    
    # you will have access to years, months, days
    
    print(f'years:{difference.years}, months:{difference.months}, days:{difference.days}')
    
    
    

    这是我想出的一个有点乱的应用程序,你会明白的

    
    
    from tkinter import Tk
    from tkinter import Label
    from tkinter import Toplevel
    from tkinter import Button
    from tkcalendar import DateEntry
    from datetime import datetime
    from datetime import date
    from dateutil.relativedelta import relativedelta
    
    
    root = Tk()
    root.title("Date picker")
    root.geometry("1000x800")
    
    currentDate = Label(root, text="current date: " + datetime.now().strftime('%Y/%m/%d'), pady=50, padx=50)
    currentDate.pack()
    
    
    
    dateInput = DateEntry(root)
    dateInput.pack()
    
    
    def destroyPopop(window):
        window.destroy()
    
    
    def calDiffence():
    
        out = relativedelta( dateInput.get_date(), date.today())
    
        return out
    
    
    def popupWindow():
        popup = Toplevel(root)
        popup.title('date difference')
        popup.geometry("400x400")
        data = calDiffence()
        diffOutput = Label(popup, text=f'years:{data.years}, months:{data.months}, days:{data.days}')
        diffOutput.pack()
    
        okButton = Button(popup, text="OK", command=lambda:destroyPopop(popup), pady=100, padx=100)
        okButton.pack()
    
        popup.pack()
    
    
    calcucate = Button(root, text="difference between the chosen date and the current date", command=popupWindow)
    calcucate.pack(pady=20)
    
    
    root.mainloop()
    
    
    
    

    【讨论】:

    • 感谢您的回答它现在有效,但它在超过 30 天后向我显示了几个月内的数据,这是我在这种情况下不想要的。
    【解决方案2】:

    我会使用内置的 python 模块 datetime。

    from datetime import date,datetime
    
    d0 = datetime.today()
    d1 = date(2008, 9, 26)
    delta = d1 - d0
    print(delta.days)
    

    【讨论】:

    • 感谢您的回答,它也可以动态工作
    • 如果我的回答比另一个好,请接受我的回答
    猜你喜欢
    • 2019-11-01
    • 1970-01-01
    • 2019-04-23
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多