Datetime in python – A quick guide with examples

datetime in python -

Hello guys! Welcome to another important and most commonly used module in python is datetime in python. This datetime module in python helps to get the date in the system and wherever we need to represent the date and time we will use the datetime in python.

Ok, let’s see what inside in datetime module in python.

To start the DateTime module in our code, we don’t need to install it. It defaults installed when you installed python. So let’s get into the datetime module directly. I was almost gone through examples to give a clear idea of it.

1. Getting the current date and time:

we can get the current system date and time using python, here I gave the two methods to get the current date and time.

# importing module
from datetime import datetime

# Method - 1
curr_date =

# Method - 2
curr_date =

# The output will be
2020-03-30 05:58:04.345318

2. Represent the date and time

we can represent the date and time manually using the Python datetime module.

from import datetime import datetime

obj = datetime(2020,3,30,12,11,30)
The output will be
2020-03-30 12:11:30

3. Represent the date

we can represent the date only if we want.

import datetime

a =,10,30)
# Output

4. Get the current date

import datetime

a =


Read our Best tutorials on python:

How to send emails using python – Complete Automation guide

How to trace a mobile number using python

5. Python datetime to Timestamp

we can coverts the Timestamps into a date using the datetime in python.

from datetime import date

Date = date.fromtimestamp(2026245375)

# Output:

6. Get Day, Month and Year

We can get day, Month and year individually using python DateTime module.

from datetime import date

obj =




# Output:

Day: 30
Month: 3
Year: 2020

7. Represents the Time

We can represent the time in the same way how we represent the date.

from datetime import time

a = time(12,1,30) # Hour,minute,seconds 
# Output:

8. Get the Hour, Minute and seconds

we can get individually hour, minute and seconds from date

from datetime import time

obj = time(12,20,45)


# Output:


9. Differences between 2 date and times

We can calculate the time difference between the 2 dates or time.

from datetime import date

t1 = date(2016,2,26)
t2 = date(2014,4,5)
diff = t1-t2


# Output:
692 days, 0:00:00

But we can’t do the same way for the time. If you want to calculate time differences then you must datetime method.

from datetime import datetime

t1 = datetime(2010,2,23,7,24,45)
t2 = datetime(2015,5,4,3,45,20)

1896 days, 3:39:25

10. Python datetime to String formates

We can convert dates in datetime module into string formates like months and days. let’s see with examples.

from datetime import datetime

t =





There are so many string formates to represent the date and time in the datetime module.

1.%Areturns full dayMonday
2.%areturns short dayMon
3.%Breturns full monthMarch
4. %breturn short monthMar
5. %Yreturns full year2020
6.%yreturns short year20
7.%Hreturns 24 Hour format13
8.%Ireturns 12 Hour format1
9. %preturns AM / PMAM
10. %Xreturns the local version of time07:06:43
11.%xreturns the local version of date03/30/20
12.%creturns the local version of time and dateMon Mar 30 06:52:57 2020

There are a lot of things in datetime module in python, I covered almost all of if. For more, please visit docs.

Ok, guys! I hope you understood how much useful datetime module to your work. This datetime module in the python tutorial helps a lot to you. If you love this tutorial then please appreciate us through comment and for more interesting tutorials please subscribe to our newsletter. Thank you.!

Leave a Reply