Write a Python program to add 5 seconds with the current time


This program is an example of how to use the datetime module to add a certain amount of time to a given datetime object. First, the program gets the current date and time using the now() method of the datetime module and assigns it to the variable a.

Then, the program creates a new datetime object b that is 5 seconds later than a. This is done by using the timedelta() method of the datetime module, which allows you to add or subtract a specific amount of time to a datetime object. In this case, the timedelta() method is called with the arguments 0 (for zero days) and 5 (for five seconds).

Finally, the program prints the time component of both a and b using the time() method of the datetime module. This method extracts the time portion of a datetime object and returns it as a time object.

Source Code

import datetime
a= datetime.datetime.now()
b = a + datetime.timedelta(0,5)
print(a.time())
print(b.time())

Output

14:47:45.687216
14:47:50.687216

Example Programs