Almost all modern and old programming languages carry the datetime module in some form or pattern and so does Python, the only difference between the datetime modules in Python and other languages is that it offers a greater variety of function and manipulation of datetime figures in Python. As all Python components are implicitly called or invoked so is the datetime module, you do not need to specify the very detailed information in order to obtain a datetime result as the built-in python module does that for you. The datetime modules contain some classes which are as follows:

Date

The date class returns a date object containing the constructors and methods for returning a date value, this value can be specified or can be obtained automatically according to the Gregorian calendar.

Time

The time class returns a time object which can be independent of the day and constructed of “hour:minute: second” format, the class contains further division of time into microseconds too.

Datetime

The name is pretty self-explanatory as it returns an object containing both time and date, both can be specified or obtained automatically according to the Gregorian calendar or the current date and time.

Timedelta

The timedelta class returns an object which can contain the difference between two time/date/datetime objects and can be precise down to the microsecond division.

 Let’s check out an example of the simple datetime object:

import datetime #This imports the entire datetime library into your Python editor

objectD = datetime.datetime.now() # We have made a separate object to call the built-in datetime method
print(objectD) #Prints the datetime object.

We can also use the date.today() method to invoke the current date only; the aforementioned block of code will invoke both the current time and date.

from datetime import date #We have only imported the date module from the datetime library

objectD = date.today() #This will only call the date of today
print(objectD) #This will print the date object.

The DateTime library also allows us to invoke specified dates which we can modify, a separate datetime.date() function is invoked:

import datetime  #We can only use the datetime library also as it contains all the relevant methods and classes.

objectD = datetime.date(2012,9,5) #This method will take in a specified date in the format YY/MM/DD
print(objectD) #This will print the date object.

We can also separate the entities from the complete date object, for example, if we have invoked the date.today() function, we can separately use the year, month and day:

from datetime import date  #In order to use the specialised constructors of the date module, we need to import it separately.

objectD = date.today() #This method will invoke the current date

print(objectD.year) #This will print the current date's year.
print(objectD.month) #This will print the current date's month.
print(objectD.day) #This will print the current date's day.

Python DateTime library also allows us to invoke a fromtimestamp() function which can obtain a date from the seconds passed from 1 January, 1970 UTC.

from datetime import date  #In order to use the specialised constructors of the date module, we need to import it separately.

objectD = date.fromtimestamp(856423) #This method will invoke the current date

print(objectD) #This will print the current date's year.

Another very important part of the Python DateTime library is the timedelta() function, which can differentiate and present the difference between two time functions, here is an example which sums the entire concept:

from datetime import timedelta #Need to import specific module from the library

delta1 = timedelta(weeks = 6, days = 3, hours = 7, minutes = 2 seconds = 30) #First delta which takes in weeks, days, hours, minutes & seconds
delta2 = timedelta(days = 1, hours = 5, minutes = 3, seconds = 40) #Second delta which takes in weeks, days, hours, minutes & seconds
delta3 = delta1 - delta2 #calculating the delta difference

print("delta3 =", delta3) #Printing the delta difference

The strftime() method also invokes the DateTime library with all the basic functions but it allows you to specifically call each entity separately in a single statement including time:

from datetime import datetime

c1 = datetime.now() #We need to call a datetime.now function inside an object to store the current time

d1 = c1.strftime("%d/%m/%Y, %H:%M:%S") #We can manipulate the datetime.now function any way we want using the c1 object. The date must be separated by / and time should be separated by :
print(d1) #Prints the entire scene

The strptime() function simply takes in a string which has a date or time in it, in a structured pattern and then transforms it into a time/date object, please note that the calling symbols are case-sensitive like %D and %d:

from datetime import datetime

dstr = "13 December, 2022, 14:54:33" #A string containing raw data of time and date

dtime = datetime.strptime(dstr, "%d %B, %Y, %H:%M:%S") #The datetime.strptime is stored inside an object, the %B represents the full name of the month, the rest is same as strftime()
print(dtime)

Categorized in: