Python Date & Time functions

Importing datetime library

from datetime import date
Today's date
from datetime import date
dt = date.today()
print(dt)    # 2019-09-16

Python date and time to get todays tomorrow yesterday dates by using relative delta for calculations

Getting day , month , year and weekday

Commonly used instances
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

Tomorrow

from datetime import timedelta, date, datetime

dt = date.today() + timedelta(days=1) # Tomorrow
print(dt)

Yesterday

dt = date.today() + timedelta(days=-1) # Yesterday

More Instance methods:

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
Creating a date object
import datetime

dt = datetime.datetime(2019, 5, 24) # Year, Month, Day
print(dt.day, dt.month, dt.year) # 24 5 2019

Difference in two dates

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

Getting time

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)

Getting Date and time

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.

Creating Date time object.
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

Present Year , previous year and Next year

The function strftime() returns string, so we have to convert the output to integer if we are doing any calculation using the output. Note the last line here.
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
calendarCalendar module to Create yearly or monthly Calendars
New YearShow Countdown and new year message
All timedelta objects relativedelta
Exercise on Date & time

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer