-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_date.py
More file actions
37 lines (28 loc) · 854 Bytes
/
Copy pathpython_date.py
File metadata and controls
37 lines (28 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from datetime import date, time, datetime, timedelta
# Current date
today = date.today()
print("Today's date:", today)
# Specific date
d = date(2024, 6, 1)
print("Specific date:", d)
# Current date and time
now = datetime.now()
print("Current date and time:", now)
# Specific date and time
dt = datetime(2024, 6, 1, 14, 30, 0)
print("Specific date and time:", dt)
# Time only
t = time(14, 30, 0)
print("Specific time:", t)
# Formatting dates
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted datetime:", formatted)
# Parsing string to datetime
parsed = datetime.strptime("2024-06-01 14:30:00", "%Y-%m-%d %H:%M:%S")
print("Parsed datetime:", parsed)
# Date arithmetic
tomorrow = today + timedelta(days=1)
print("Tomorrow's date:", tomorrow)
# Difference between dates
delta = tomorrow - today
print("Difference in days:", delta.days)