Datetime module in Python
This article discusses the implementation of the datetime module in the python language. This datetime module has a function for data processing against date and time. For example, if we want to get the time or date data today.
Just like other modules available in python, the functions in the datetime module must be called first with the command:
import datetime
from datetime import *
After calling the datetime module in our project, we can use the functions in the datetime module, the functions include:
Function today()
the today() function is used to display the current date
import datetimetoday_date = datetime.date.today()print(today_date) #the output is displaying today's date
The output is…
2021-12-17
instead of using today() , we can use now()
Function now()
the difference is that the now() function displays more detailed output.
import datetimetoday_date = datetime.datetime.now()print(today_date) #the output is displaying today's date
The above example gives the output…
2021-12-17 19:54:34.030162
Retrieve year, month and day data
in the now() and today() functions, we can also access the data separately, according to the format we want.
from datetime import datetoday_date = date.today()print("This year :", today_date.year)print("This month :", today_date.month)print("today's date:", today_date.day)
Checkout the outputs
This year : 2021
This month : 12
today's date: 17
let’s try using the now() function…
import datetimetoday_date = datetime.datetime.now()print("This year :", today_date.year)
print("This month :", today_date.month)
print("today's date:", today_date.day)
print("hour :", today_date.hour)
print("minute :", today_date.minute)
print("second :", today_date.second)
print("microsecond :", today_date.microsecond)
The examples return the output..
This year : 2021
This month : 12
today's date: 17
hour : 20 #20:11:33.857901
minute : 11
second : 33
microsecond : 857901
Function timedelta()
timedelta() function is used to calculate the difference on a certain date.
from datetime import datetoday_date = datetime.date(datetime.now())
print('Today : ', today_date)last_week1 = today_date + timedelta(days=-7)
#or
last_week2 = today_date - timedelta(days=7)
#or
last_week3 = today_date + timedelta(weeks=-1)print(last_week1)
print(last_week2)
print(last_week3)
Output :
Today : 2021-12-17
2021-12-10
2021-12-10
2021-12-10
or we can use ..
from datetime import datebirth_day = date(year = 2010, month = 10, day = 10)today_date = date.today()
subtract_date = today_date - birth_day
print('Today : ', today_date)
print('Birthday : ', birth_day)
print('My age : ', substract_date.days, 'days')
returns the output..
Today : 2021-12-17
Birthday : 2010-10-10
My age : 4086 days
Changing the time format
from datetime import datetimetoday_date = datetime.now()# format dd/mm/YY
date_x = today_date.strftime('%d/%m/%Y')
print('date :', date_x)
# format dd/mm/YY H:M:S
time_date = today_date.strftime("%d/%m/%Y, %H:%M:%S")
print('date and time: ', time_date)
The above example gives the output…
date : 17/12/2021
date and time: 17/12/2021, 20:45:43
strptime() : string parse time
This function is used to convert data in the form of a string, into a datetime form with the desired format
from datetime import datetime
ex_date = '17-08-1945'#to prove that the data type is string
print(type(ex_date))#data conversion, from str to datetime , with that format
ex_date = datetime.strptime(ex_date,'%d-%m-%Y')#to prove that the data type is datetime
print(type(ex_date))
Checkout the output…
<class 'str'>
1945-08-17 00:00:00
<class 'datetime.datetime'>
strftime() : string format time
This function is the opposite of the strptime() function, this function is used to convert datetime data into data of type string , with the specified date format.
from datetime import datetime
today_date = datetime.now()print(today_date)
print(type(today_date))string_date = datetime.strftime(today_date,'%d-%b-%Y %H:%M:%S')print(string_date)
print(type(string_date))
The output is…
2021-12-17 21:00:46.176106
<class 'datetime.datetime'>
17/Dec/2021 21:00:46
<class 'str'>
THANK YOU !!!
images reference :
https://tutorial.eyehunts.com/python/python-datetime-strptime-convert-string-to-datetime-example/
https://tutorial.eyehunts.com/python/python-strftime-function-milliseconds-examples/
https://tutorial.eyehunts.com/python/python-timedelta-difference-two-date-time-datetime/