Datetime Module:
Date and time are not a data type of their own, but a module named datetime can be imported to work with the date as well as time.
Python Datetime module comes built into Python, so there is no need to install it externally.
date – An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Its attributes are year, month, and day.
time – An idealized time, independent of any particular day, assuming that every day has exactly 246060 seconds. Its attributes are hour, minute, second, and microsecond.
datetime – Its a combination of date and time along with the attributes year, month, day, hour, minute, second, and microsecond.
timedelta – A duration expressing the difference between two date, time, or datetime instances to microsecond resolution.
Import datetime module:
- We can use keyword import to import built-in datetime module
import datetime
current_time = datetime.datetime.now()
print("current_time: ",current_time)
# time attributes for dt_now
print("Current hour:", current_time.hour)
print("Current minute:", current_time.minute)
print("Current second:", current_time.second)
Output: current_time: 2024-08-21 07:00:46.752592 Current hour: 7 Current minute: 0 Current second: 46
N.B:
- datetime (first occurrence): This refers to the module itself, which you import with import datetime. The datetime module contains various classes and functions related to date and time.
- datetime (second occurrence): This refers to the datetime class within the datetime module. The datetime class represents date and time objects.
- The current date above contains year, month, day, hour, minute, second, and microsecond.
Create a date object:
- We can use date class to create date object
from datetime import date
ShahRukh_birthday = date(1965,11,2)
print("ShahRukh_birthday: ",ShahRukh_birthday)
Output: ShahRukh_birthday: 1965-11-02
Getting date:
- We can use date class to know today’s date
from datetime import date
today_date = date.today()
print("today_date: ",today_date)
# Get Today’s Year, Month, and Date
print("Current year:", today_date.year)
print("Current month:", today_date.month)
print("Current day:", today_date.day)
Output: today_date: 2024-08-21 Current year: 2024 Current month: 8 Current day: 21
timedelta:
- timedelta is a class in Python’s datetime module that represents a duration or the difference between two dates, times, or datetimes.
- It is useful for performing arithmetic operations on date and time objects, such as adding or subtracting time intervals.
- We can create a timedelta object by specifying the duration in days, seconds, microseconds, milliseconds, minutes, hours, or weeks.
from datetime import timedelta,date
# Create a timedelta representing time period
Course_duration = timedelta(days=730 ,hours=18, minutes=56)
print("Course_duration :", Course_duration)
Course_start_date = date(2024,8,21)
print("Course_start_date :",Course_start_date)
# Adding course duration to get course ending date
Course_end_date = Course_start_date + Course_duration
print("Course_end_date:", Course_end_date)
# Subtract last 6 months to get applied date
Applied_date = Course_start_date - timedelta(weeks=24)
print("Applied_date:", Applied_date)
Output: Course_duration : 730 days, 18:56:00 Course_start_date : 2024-08-21 Course_end_date: 2026-08-21 Applied_date: 2024-03-06
Date format to string format:
- We can use strftime() for formatting date objects into readable strings.
today_date = date.today()
print("today_date: ",today_date)
Month = today_date.strftime("%B")
print("Month:",Month)
Month_short = today_date.strftime("%b")
print("Month:",Month_short)
Day = today_date.strftime("%A")
print("Day: ",Day)
Day_short = today_date.strftime("%a")
print("Day: ",Day_short)
Year = today_date.strftime("%Y")
print("Year: ",Year)
Year_short = today_date.strftime("%y")
print("Year: ",Year_short)
Output: today_date: 2024-08-21 Month: August Month: Aug Day: Wednesday Day: Wed Year: 2024 Year: 24
String format to a Date format:
- We can convert a string to a date object using the strptime method from the datetime class within the datetime module.
from datetime import datetime
# Example date string
date_string = "2024-08-21"
# Convert the string to a date object
date_object = datetime.strptime(date_string, "%Y-%m-%d")
print("Date object:", date_object)
Output: Date object: 2024-08-21 00:00:00