from datetime import date
Today's date
from datetime import date
dt = date.today()
print(dt) # 2019-09-16
from datetime import date
print("Today day : ", date.today().day)
print("Today month : ", date.today().month)
print("Today year : ", date.today().year)
print("Today weekday : ", date.today().weekday())
Output
Today day : 16
Today month : 9
Today year : 2019
Today weekday : 0
from datetime import timedelta, date, datetime
dt = date.today() + timedelta(days=1) # Tomorrow
print(dt)
dt = date.today() + timedelta(days=-1) # Yesterday
toodinal() | Proleptic Gregorian ordinal of the date |
timetuple() | Details of date and time as a tuple |
strftime() | Date & time to String |
strptime() | String to datetime object |
resolution() | Minimum difference between String to datetime object |
replace() | Update date object with new parameters |
max() & min() | Maximum and Minimum representable date |
isoweekday() | Day of the week as integer |
isocalendar() | ISO year, ISO week number & ISO weekday |
fromtimestamp() | Getting date and time by using timestamp |
fromordinal() | Getting date by using ordinal |
fromisoformat() | Getting date by using iso fomat date string |
import datetime
dt = datetime.datetime(2019, 5, 24) # Year, Month, Day
print(dt.day, dt.month, dt.year) # 24 5 2019
from datetime import date
d1 = date(2022, 6, 30) # Year, Month, Day
d2 = date(2023, 2, 16) # Year, Month, Day
diff_days = d2 - d1
print('Difference in days :', diff_days.days) # Difference in days: 231
import time
h = time.strftime('%I') # Getting local hour in 12-hour format as a string
h = int(h) * 2 # Convert to integer and multiply by 2
print(h)
from datetime import datetime
now = datetime.now()
print(now) # Present date and time
# Use different formats
dt = datetime.strftime(now, '%Y/%m/%d : %H:%M:%S')
print(dt)
Output
2021-12-22 06:27:52.051532
2021/12/22 : 06:27:52
Read more on strftime() formats to get hour minutes etc.
import datetime
dt = datetime.datetime(2019, 5, 24, 14, 52, 53)
print(dt.day, dt.month, dt.year) # 24 5 2019
print(dt.hour, dt.minute, dt.second) # 14 52 53
Output
24 5 2019
14 52 53
import datetime
today = datetime.datetime.now() # Creating date object
print("Current Year is :", today.year) # Current year as integer
year1 = today.year # The year part or the year attribute
print("Next Year is : ", year1 + 1) # Year is an integer here.
year2 = today.strftime("%Y") # Returns year as a string
print("Previous Year is : ", int(year2) - 1) # Converts to integer then subtract
2nd Saturday | Display 2nd and 4th Saturday of the month |
calendar | Calendar module to Create yearly or monthly Calendars |
New Year | Show Countdown and new year message |
AUTHOR
🎥 Join me live on YouTubePassionate about coding and teaching, I love sharing practical programming tutorials on PHP, Python, JavaScript, SQL, and web development. With years of experience, my goal is to make learning simple, engaging, and project-oriented. Whether you're a beginner or an experienced developer, I believe learning by doing is the best way to master coding. Let's explore the world of programming together!