Curriculum
In this tutorial, you will learn about Date and time in Python with various use cases and how to use them in your Python application.
To get the date and time in python, you need to import the datetime module. It is a built-in module that helps you to work with date and time related operations.
To create date and time objects, first, you need to import the datetime module. The datetime objects should have three arguments in the order year, month, day (YYYY-MM-DD).
Syntax
Datetime.date (yyyy,mm,dd)
To get the current date and time in python, you can use the now() function of the datetime.date class.
import datetime todays_date=datetime.date.today() print(todays_date) current_time=datetime.datetime.now() print("current_time",current_time)
2022-02-18 current_time 2022-02-18 04:11:25.069777
To get the current date alone in your python program, you need to call just today’s date.
Input
import datetime todays_date=datetime.date.today() print(todays_date)
Output:
2022-02-18
Using the strftime() function, you can format the datetime in python. The strftime(), will take one parameter, format, to specify the format of the returned string.
Input
import datetime current_date = datetime.date.today() print(current_date.strftime("%a"))
Output:
Fri
Similar to strftime() the strptime, you can format the datetime in python. Using the strptime() you can convert a given string to datetime object.
Input:
from datetime import datetime date = ("17 February, 2022" ) date_object = datetime.strptime(date, "%d %B, %Y") print("date_object =", date_object)
Note:
In the above code:
Output:
date_object = 2022-02-17 00:00:00
You can create a time object by using the datetime module. This object will return the time stated or the current time. And using the time object you can print the variables in time like hours, minutes, and seconds.
Input
from datetime import datetime current_time=datetime.now() print("current_time",current_time) print("hour:",current_time.hour) print("Minute:",current_time.minute) print("Seconds:", current_time.second)
Output:
current_time 2022-02-18 04:24:24.878797 hour: 4 Minute: 24 Seconds: 24
The timestamp is calculated by the total no. of seconds from 1st January 1970 at UTC to a particular date.
To get date from timestamp,
from datetime import datetime timestamp = 575757575 date = datetime.fromtimestamp(timestamp) print("Date : ", date)
Output:
Date : 1988-03-30 20:39:35
The timedelta() function is an inuilt function in python. It is associated with datetime library and it is generally used for calculating differences in dates .
Syntax : datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
Returns : Date
Sample program:
from datetime import datetime, timedelta current_date = datetime.now() print ("Initial_date", str(current_date)) after_5yrs = current_date + timedelta(days = 1826) print('Affter 5yrs:', str(after_5yrs))
Ouput:
Initial_date 2022-02-18 04:27:54.416117 Affter 5yrs: 2027-02-18 04:27:54.416117
The dir() function in python lists out all the attributes and methods in any object.
Example:
import datetime print(dir(datetime))
Output:
['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']