Python provides the datetime module work with actual dates and times. In real-world applications, we want to work with the date and time. Python permits us to time table our Python script to run at a specific timing.
In Python, the date is not a information type, however we can work with the date objects via importing the module named with datetime, time, and calendar.
In this part of the tutorial, we will talk about how to work with the date and time objects in Python.
The datetime classes are categorised in the six essential classes.
date – It is a naive ideal date. It consists of the year, month, and day as attributes.
time – It is a perfect time, assuming every day has precisely 24*60*60 seconds. It has hour, minute, second, microsecond, and tzinfo as attributes.
datetime – It is a grouping of date and time, along with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.
timedelta – It represents the difference between two dates, time or datetime instances to microsecond resolution.
tzinfo – It provides time zone information objects.
timezone – It is included in the new version of Python. It is the class that implements the tzinfo abstract base class.
Tick
In Python, the time instants are counted given that 12 AM, 1st January 1970. The characteristic time() of the module time returns the complete number of ticks spent due to the fact 12 AM, 1st January 1970. A tick can be considered as the smallest unit to measure the time.
Consider the following example
import time;
#prints the number of ticks spent since 12 AM, 1st January 1970
print(time.time())
Output:
1585928913.6519969
How to get the current time?
The localtime() features of the time module are used to get the present day time tuple. Consider the following example.
Example
import time;
#returns a time tuple
print(time.localtime(time.time()))
Output:
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=3, tm_hour=21, tm_min=21, tm_sec=40, tm_wday=4, tm_yday=94, tm_isdst=0)
Time tuple
The time is handled as the tuple of 9 numbers. Let’s seem to be at the participants of the time tuple.
Index | Attribute | Values |
---|---|---|
0 | Year | 4 digit (for example 2018) |
1 | Month | 1 to 12 |
2 | Day | 1 to 31 |
3 | Hour | 0 to 23 |
4 | Minute | 0 to 59 |
5 | Second | 0 to 60 |
6 | Day of weak | 0 to 6 |
7 | Day of year | 1 to 366 |
8 | Daylight savings | -1, 0, 1 , or -1 |
Getting formatted time
The time can be formatted with the aid of the usage of the asctime() function of the time module. It returns the formatted time for the time tuple being passed.
Example
import time
#returns the formatted time
print(time.asctime(time.localtime(time.time())))
Output:
Tue Dec 18 15:31:39 2018
Python sleep time
The sleep() approach of time module is used to end the execution of the script for a given amount of time. The output will be delayed for the range of seconds supplied as the float.
Consider the following example.
Example
import time
for i in range(0,5):
print(i)
#Each element will be printed after 1 second
time.sleep(1)
Output:
0
1
2
3
4
The datetime Module
The datetime module permits us to create the customized date objects, perform more than a few operations on dates like the comparison, etc.
To work with dates as date objects, we have to import the datetime module into the python supply code.
Consider the following example to get the datetime object representation for the cutting-edge time.
Example
import datetime
#returns the current datetime object
print(datetime.datetime.now())
Output:
2020-04-04 13:18:35.252578
Creating date objects
We can create the date objects bypassing the preferred date in the datetime constructor for which the date objects are to be created.
Consider the following example.
Example
import datetime
#returns the datetime object for the specified date
print(datetime.datetime(2020,04,04))
Output:
2020-04-04 00:00:00
We can also specify the time alongside with the date to create the datetime object. Consider the following example.
Example
import datetime
#returns the datetime object for the specified time
print(datetime.datetime(2020,4,4,1,26,40))
Output:
2020-04-04 01:26:40
In the above code, we have exceeded in datetime() feature year, month, day, hour, minute, and millisecond attributes in a sequential manner.
Comparison of two dates
We can compare two dates by way of the use of the evaluation operators like >, >=, <, and <=.
Consider the following example.
Example
from datetime import datetime as dt
#Compares the time. If the time is in between 8AM and 4PM, then it prints working hours otherwise it prints fun hours
if dt(dt.now().year,dt.now().month,dt.now().day,8)<dt.now()<dt(dt.now().year,dt.now().month,dt.now().day,16):
print("Working hours....")
else:
print("fun hours")
Output:
fun hours
The calendar module
Python offers a calendar object that incorporates a number of methods to work with the calendars.
Consider the following example to print the calendar for the final month of 2018.
Example
import calendar;
cal = calendar.month(2020,3)
#printing the calendar of December 2018
print(cal)
Output:
March 2020
Mo Tu We Th Fr Sa Su
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Printing the calendar of whole year
The prcal() approach of calendar module is used to print the calendar of the whole year. The 12 months of which the calendar is to be printed ought to be surpassed into this method.
Example
import calendar
#printing the calendar of the year 2019
s = calendar.prcal(2020)
Output:

Leave a Review