Skip to content

Commit 2a1e1fd

Browse files
authored
Create Date-difference-calculator.py
1 parent 1c5040c commit 2a1e1fd

1 file changed

Lines changed: 223 additions & 0 deletions

File tree

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
import sys
2+
import os
3+
import tkinter as tk
4+
from tkinter import messagebox
5+
from tkcalendar import DateEntry
6+
from dateutil.relativedelta import relativedelta
7+
8+
# =========================
9+
# THEME
10+
# =========================
11+
APP_BG = "#121212"
12+
PANEL_BG = "#1F1F1F"
13+
BTN_BG = "#2C2C2C"
14+
ACCENT = "#FF6F61"
15+
TEXT_CLR = "#E0E0E0"
16+
SUBTEXT_CLR = "#AAAAAA"
17+
18+
FONT = ("Segoe UI", 11)
19+
20+
# =========================
21+
# RESOURCE PATH
22+
# =========================
23+
def resource_path(file_name):
24+
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
25+
return os.path.join(base_path, file_name)
26+
27+
# =========================
28+
# DATE LOGIC (CALENDAR-ACCURATE)
29+
# =========================
30+
def calculate_exact_difference(d1, d2):
31+
if d2 < d1:
32+
d1, d2 = d2, d1
33+
34+
delta = relativedelta(d2, d1)
35+
total_days = (d2 - d1).days
36+
37+
return {
38+
"Years": delta.years,
39+
"Months": delta.months,
40+
"Days": delta.days,
41+
"Total Days": total_days
42+
}
43+
44+
# =========================
45+
# APP
46+
# =========================
47+
class DateDifferenceCalculator:
48+
def __init__(self, root):
49+
self.root = root
50+
root.title("MateTools – Date Difference Calculator")
51+
root.geometry("1000x520")
52+
root.configure(bg=APP_BG)
53+
root.resizable(False, False)
54+
55+
# =========================
56+
# LEFT PANEL
57+
# =========================
58+
left = tk.Frame(root, bg=PANEL_BG, width=420)
59+
left.pack(side="left", fill="y")
60+
61+
# =========================
62+
# LEFT HEADER
63+
# =========================
64+
header = tk.Frame(left, bg=PANEL_BG)
65+
header.pack(fill="x", padx=16, pady=(18, 10))
66+
67+
tk.Label(
68+
header,
69+
text="MateTools",
70+
bg=PANEL_BG,
71+
fg=ACCENT,
72+
font=("Segoe UI", 20, "bold")
73+
).pack(side="left")
74+
75+
# Divider
76+
tk.Frame(left, bg=ACCENT, height=2).pack(fill="x", padx=16, pady=(0, 14))
77+
78+
# =========================
79+
# SUB TOOL TITLE (LEFT)
80+
# =========================
81+
tk.Label(
82+
left,
83+
text="Date Difference Calculator",
84+
bg=PANEL_BG,
85+
fg=TEXT_CLR,
86+
font=("Segoe UI", 14, "bold")
87+
).pack(anchor="w", padx=16, pady=(0, 2))
88+
89+
tk.Label(
90+
left,
91+
text="Calendar-accurate years, months, and days",
92+
bg=PANEL_BG,
93+
fg=SUBTEXT_CLR,
94+
font=("Segoe UI", 10)
95+
).pack(anchor="w", padx=16, pady=(0, 16))
96+
97+
tk.Frame(left, bg=BTN_BG, height=1).pack(fill="x", padx=16, pady=(0, 16))
98+
99+
# =========================
100+
# DATE INPUTS
101+
# =========================
102+
tk.Label(
103+
left,
104+
text="Select Dates",
105+
bg=PANEL_BG,
106+
fg=TEXT_CLR,
107+
font=("Segoe UI", 12, "bold")
108+
).pack(anchor="w", padx=16, pady=(0, 6))
109+
110+
self.date1 = DateEntry(
111+
left,
112+
background=BTN_BG,
113+
foreground=TEXT_CLR,
114+
borderwidth=0,
115+
font=FONT,
116+
date_pattern="yyyy-mm-dd"
117+
)
118+
self.date1.pack(fill="x", padx=16, pady=8)
119+
120+
self.date2 = DateEntry(
121+
left,
122+
background=BTN_BG,
123+
foreground=TEXT_CLR,
124+
borderwidth=0,
125+
font=FONT,
126+
date_pattern="yyyy-mm-dd"
127+
)
128+
self.date2.pack(fill="x", padx=16, pady=8)
129+
130+
# =========================
131+
# BUTTONS
132+
# =========================
133+
btn_frame = tk.Frame(left, bg=PANEL_BG)
134+
btn_frame.pack(fill="x", padx=16, pady=16)
135+
136+
def make_btn(text, cmd, color=BTN_BG):
137+
return tk.Button(
138+
btn_frame,
139+
text=text,
140+
command=cmd,
141+
bg=color,
142+
fg="white",
143+
font=("Segoe UI", 11, "bold"),
144+
relief="flat",
145+
height=2,
146+
width=18
147+
)
148+
149+
make_btn("Calculate", self.calculate).pack(side="left", expand=True, padx=4)
150+
make_btn("About", self.show_about, ACCENT).pack(side="left", expand=True, padx=4)
151+
152+
# =========================
153+
# RIGHT PANEL
154+
# =========================
155+
right = tk.Frame(root, bg=APP_BG)
156+
right.pack(side="right", fill="both", expand=True)
157+
158+
# =========================
159+
# STATS CARD
160+
# =========================
161+
stats_card = tk.Frame(right, bg=PANEL_BG)
162+
stats_card.pack(padx=30, pady=40, fill="both", expand=True)
163+
164+
tk.Label(
165+
stats_card,
166+
text="Exact Date Difference",
167+
bg=PANEL_BG,
168+
fg=TEXT_CLR,
169+
font=("Segoe UI", 14, "bold")
170+
).pack(pady=(20, 10))
171+
172+
self.stats = {}
173+
for label in ["Years", "Months", "Days", "Total Days"]:
174+
frame = tk.Frame(stats_card, bg=PANEL_BG)
175+
frame.pack(fill="x", padx=40, pady=6)
176+
177+
tk.Label(
178+
frame,
179+
text=label,
180+
bg=PANEL_BG,
181+
fg=SUBTEXT_CLR,
182+
font=("Segoe UI", 11)
183+
).pack(side="left")
184+
185+
value = tk.Label(
186+
frame,
187+
text="0",
188+
bg=PANEL_BG,
189+
fg="white",
190+
font=("Segoe UI", 12, "bold")
191+
)
192+
value.pack(side="right")
193+
194+
self.stats[label] = value
195+
196+
# =========================
197+
# METHODS
198+
# =========================
199+
def calculate(self):
200+
d1 = self.date1.get_date()
201+
d2 = self.date2.get_date()
202+
203+
result = calculate_exact_difference(d1, d2)
204+
for key, value in result.items():
205+
self.stats[key].config(text=str(value))
206+
207+
def show_about(self):
208+
messagebox.showinfo(
209+
"About",
210+
"MateTools – Date Difference Calculator\n\n"
211+
"• Calendar picker input\n"
212+
"• Calendar-accurate calculations\n"
213+
"• Leap years supported\n\n"
214+
"Built by MateTools"
215+
)
216+
217+
# =========================
218+
# RUN
219+
# =========================
220+
if __name__ == "__main__":
221+
root = tk.Tk()
222+
DateDifferenceCalculator(root)
223+
root.mainloop()

0 commit comments

Comments
 (0)