DateTime module in Python is the equivalent of date and time in the human world. DateTime objects are used to work with Date and Time in Python programming. Five classes can be used if you are required to work with date and time objects. The classes are:
date: That contains only date information eg. (year, month, day) -> (2023, 09, 18)
time: That is independent of the day eg. (hour, minute, second, microsecond) -> (11,20,10,5)
datetime: That combines both datetime information.
timedelta: This represents the difference between two dates or time.
tzinfo: This helps to manipulate timezone.
Date and Time objects will be classified as Naive or Aware based on whether they include the timezone information or not. (
Naive datetimes don't have enough information to determine the timezone or to determine whether there is daylight saving or not. For eg: Let's assume that we have the number 10. Now we don't know what that number it can be your roll number in school or it can be a house in Downing Street. We can't be sure about it without having more information.
This is when we use.....
Aware datetime has enough information to keep track of timezone and daylight savings. They are a bit complex to work out, but is very important if your program needs timezone information.
Let us understand this further with the code:
from datetime import datetime #pytz is used for timezone import pytz tz = pytz.timezone('Australia/Sydney') #used for aware time sydney_time = datetime.now(tz) print("Sydney Time",sydney_time) #used for naive time naive = datetime.now() print("Naive time:", naive)
From the above output, you can see the difference between the two classes of datetime.