Add seconds, minutes, hours and days in python

This article teaches you how to add seconds, minutes, hours, and days to the datetime object in python. The datetime module in python has a special class called timedelta. This class enables us to make these operations to the datetime object and also return the datetime object.

The following topics are discussed here.

  1. Adding seconds to a datetime object.
  2. Adding minutes to a datetime object.
  3. Adding hours to datetime object.
  4. Adding days to datetime object.

Let us look at the above concepts one by one with an example for each.

1. Adding seconds to datetime object

The timedelta takes an argument called seconds. We can specify the number of seconds we want to add to the datetime object here.

from datetime import datetime, timedelta

# add seconds

print(datetime.now())
print(datetime.now() + timedelta(seconds=10))

The output of the above code is,

2020-08-22 20:29:41.249383
2020-08-22 20:29:51.249470

As you can see 10 seconds is added to the current datetime object.

2. Adding minutes to datetime object

Similarly we can also add minutes to the datetime object by providing the minutes keyword. This will add the specified minutes to the current datetime object.

from datetime import datetime, timedelta

# add minutes
print(datetime.now())
print(datetime.now() + timedelta(minutes=20))

The output of the above code is,

2020-08-22 20:33:51.808385
2020-08-22 20:53:51.808516

20 minutes is added to the current datetime object.

3. Adding hours to datetime object

The timedelta class also supports an hours keyword which will add the given value in hours to the datetime object.

from datetime import datetime, timedelta

# add hours
print(datetime.now())
print(datetime.now() + timedelta(hours=1))

The output of the above code is,

2020-08-22 20:36:51.988043
2020-08-22 21:36:51.988110

One hour is added to the current datetime object.

4. Adding days to datetime object

The timedelta class also supports a days keyword which will add the given value in days to the datetime object.

from datetime import datetime, timedelta

# add days
print(datetime.now())
print(datetime.now() + timedelta(days=1))

The output of the above code is,

2020-08-22 20:39:43.983337
2020-08-23 20:39:43.983452

One day is added to the current datetime object.

However the timedelta class does not provide a way to add months or years to the datetime object. If you want to add month or years to the current datetime object, convert them to days and then you can add it to the current datetime object like this.

from datetime import datetime, timedelta

# add seconds
print(datetime.now())
print(datetime.now() + timedelta(days=30))
print(datetime.now() + timedelta(days=365))

The output of the above code is,

2020-08-22 20:43:37.600518
2020-09-21 20:43:37.600579
2021-08-22 20:43:37.600599

As you can see one month and one year is added to the current datetime object.

Conclusion

Hope this article is helpful. If you have any doubts leave them below in the comment box.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *