DEV Community

Cover image for How to work with datetime module in python
lihini223
lihini223

Posted on

How to work with datetime module in python

Since a date is not a data type in python on its own, we have to use the predefined python module called datetime to work with dates as date objects. Here I am going to explain the most important and frequently used classes from the module.

First of all, we need to import the classes from the datatime module to use them. Here I have import date, time and datetime classes which we are going to talk about later.

from datetime import date
from datetime import time
from datetime import datetime
Enter fullscreen mode Exit fullscreen mode

These are the predefined functionalities in the python library that let us manipulate dates and times. We don’t have to write codes because the required code pieces are already written for us. We just have to import and use them in our code. Let’s get started to use some of the features.

DATE OBJECTS

Get today’s date from the today ( ) method from the date class

In this code we call the today ( ) method in the date class which returns the information about the current date. The variable today holds the object that comes back from the date.today() function.

  today = date.today()
  print ("Today's date is ", today)
Enter fullscreen mode Exit fullscreen mode

The output :

today is  2021-01-23
Enter fullscreen mode Exit fullscreen mode

Print out the date’s individual components
The object that comes from the date.today function has several properties such as day, month, and year. I am going to print out the current day, month, and year in the below code.

 print ("Date Components: ", today.day, today.month, today.year)
Enter fullscreen mode Exit fullscreen mode

The Output:

Date Components 23 1 2021
Enter fullscreen mode Exit fullscreen mode

Also, this has some other features which we can use to develop advanced features for an app. For example, We can retrieve the weekday number as follows.

Retrieve today's weekday (0=Monday, 6=Sunday)

Today. weekday ( ) function will return a number which is according to the current weekday. Weekday numbers start with 0 for Monday as follows.

[ 0: “Monday”, 1: “Tuesday”, 2: “Wednesday”, 3: “Thursday”, 4: “Friday”,
5: “Saturday”, 6: “Sunday” ]

We can use the weekday number as an index and print out the relevant day.

  print ("Today's Weekday #: ", today.weekday())

  days=["monday","tuesday","wednesday","thursday","friday",
  "saturday","sunday"]

  print ("Which is a " + days[today.weekday()])
Enter fullscreen mode Exit fullscreen mode

The Output:

Today's weekday 5
Today's' saturday
Enter fullscreen mode Exit fullscreen mode

DATETIME OBJECTS

Get today’s date from the datetime class

Instead of using date class, we can also use the datetime class as well. This class returns an object that contains the date along with the time. In the below code I have called the now ( ) function which returns the current date and time.

  today = datetime.now()
  print  ("The current date and time is ", today)
Enter fullscreen mode Exit fullscreen mode

In the output below we have got the date, hours, minutes, seconds, and milliseconds.

today is 2021-01-23 01:44:35.870833
Enter fullscreen mode Exit fullscreen mode

Get the current time

To get the time current time we need to get the time portion of the datetime object. In the below code t equals datetime.time. What I do here is I pass the results of datetime.now ( ) function to the datetime.time ( ) function. Then the datetime.time ( ) will get the time portion from the datetime.now ( ) result and assign it to t.

  t = datetime.time(datetime.now())
  print ("The current time is ", t)
Enter fullscreen mode Exit fullscreen mode

In the output, we have got jus the time

The current time is 01:44:35.870833
Enter fullscreen mode Exit fullscreen mode

To learn more about datetime module you can refer following links.
https://docs.python.org/3/library/datetime.html
https://www.w3schools.com/python/python_datetime.asp
https://www.programiz.com/python-programming/datetime

Happy Coding !!!

Top comments (0)