Datetime in python – A quick guide with examples

datetime in python - www.devpyjp.com

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 = datetime.now()

# Method - 2
curr_date = datetime.today()

print(curr_datetime)
# 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)
print(obj)
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 = datetime.date(2020,10,30)
print(a)
# Output
2020-10-30

4. Get the current date

import datetime

a = datetime.date.today()
print(a)

#Output:
2020-03-30

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)

print(Date)
# Output:
2034-03-18

6. Get Day, Month and Year

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

from datetime import date

obj = date.today()

print('Day:',obj.day)

print('Month:',obj.month)

print('Year:',obj.year)

# 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 
print(a)
# Output:
12:01:30

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)

print(obj)

print('Hour:',obj.hour)
print('Minute:',obj.minute)
print('Seconds:',obj.second)
# Output:
12:20:45

Hour:12
Minute:20
Seconds:45

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

print(diff)

# 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)
print(t2-t1)

#Output:
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 = datetime.now()

t.strftime("%A")
t.strftime("%a")

t.strftime("%B")
t.strftime("%b")

#Output:
'Monday'
'Mon'

'March'
'Mar'

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

S.NoCodeDescriptionExample
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