Date and Time in Python
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.
Date and Time in Python
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.
How to Create Date time object in Python?
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)
How do you get the current date and time?
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)
Output:
2022-02-18 current_time 2022-02-18 04:11:25.069777
How to get only the current date without time in Python?
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
strftime in Python
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
Format the datetime using strptime
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:
- %d – day of the month.
- %B – the name of the month.
- %Y – Year in YYYY format.
Output:
date_object = 2022-02-17 00:00:00
Time object in Python
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
How to get date from timestamp?
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
TimeDelta in Python
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
dir() function and Date Time in Python
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']