Add Seconds to Datetime in Python

Add seconds to datetime in Python

Date and Time in Python

Python provides us with the datetime module that can be used to create datetime objects which can store and manipulate with date and time. This module is equipped with a lot of functionalities to work with such values with ease. We can specify all the attributes of time up to microseconds with this module

Ways to add seconds to datetime object in Python

We will now discuss how to add seconds to datetime in Python.

Using the datetime.timedelta object to add seconds to datetime in Python

We cannot directly add or subtract two datetime objects in Python. For such cases, we use the datetime.timedelta object to represent some difference between two datetime objects.

We can add seconds to datetime in Python using this timedelta class. We will create a timedelta object and specify only its seconds attribute. On adding this to an existing datetime object, we will observe that the seconds have been added up.

See the code below.

Output:

2022-05-09 15:20:20
20

Using the datetime.time.second attribute to add seconds to datetime in Python

As discussed earlier, we can add attributes up to microseconds in the datetime object. We can add the desired seconds while creating the datetime object.

We can specify the attributes for date and time in the datetime constructor. For the date, we have the year, month, and day attributes.

For specifying the time, we have the hour, minute, second, and microsecond attributes.

We will specify the attributes till second to add seconds to datetime in Python.

For example,

Output:

2022-05-09 15:20:15
15

Using the arrow.shift() function to add seconds to arrow object in Python

This method does not add time to a datetime object but rather uses a different module called the arrow. The arrow module also deals with the date and time objects efficiently. It provides an efficient way to work with such values and was built as an improvement to the datetime module with new features.

We can use the shift() function from this module can update a given arrow object. We can update the year, month, day, or any other value using this function.

To add seconds, we use the seconds parameter within the shift() function.

For example,

Output:

2022-02-09T06:19:11.520930+05:30

Conclusion

In this tutorial, we discussed how to add seconds to datetime in Python. We discussed datetime objects and how we can show seconds in this object. We can add seconds to datetime in Python using the datetime.timedelta object. For this, we create a timedelta object containing only seconds. A different module for the same, called arrow, was also demonstrated. The arrow.shift() function can be used to add seconds to an arrow object.

That’s all about how to add seconds to datetime in Python.

Was this post helpful?

Related Posts

  • 23 September

    TypeError: Object of Type Datetime Is Not Json Serializable in Python

    Table of ContentsFix the object of type datetime is not JSON serializable exception in PythonUsing the default parameter in the json.dumps() functionUsing the cls parameter in the json.dumps() functionUsing the str functionConclusion In Python, the datetime library allows us to create objects of the datetime class that can store and process date and time values […]

  • 23 September

    Convert Datetime to Unix Timestamp in Python

    Table of ContentsDates and Unix TimestampWays to convert datetime to Unix timestampUsing the total_seconds() function to convert datetime to Unix timestamp in PythonUsing the timestamp() function to convert datetime to Unix timestamp in PythonUsing the strftime() function to convert datetime to Unix timestamp in PythonUsing the time.mktime() function to convert datetime to Unix timestamp in […]

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 18 May

    Get Hour from datetime in Python

    Table of ContentsHow to get hour from datetime in Python?Using the time() function and manually slicing out the hour part to get hour from datetime in Python.Using the % operator along with the datetime.hour attribute to get hour from datetime in Python.Using the format() function along with the datetime.hour attribute to get hour from datetime […]

  • 18 May

    Convert Month Name to Number in Python

    Table of ContentsHow to convert month name to number in Python?Creating a user-defined function and utilizing it to convert month name to number in Python.Using the calendar module to create a dictionary and utilize it to convert month name to number in Python.Using dict comprehension and the calendar module to convert month name to number […]

  • 18 May

    Add Hours to datetime in Python

    Table of Contentsdatetime in PythonWays to add hours to datetime in PythonUsing the datetime.Hour Attribute to Add Hours to datetime in PythonUsing the datetime.timedelta object to add hours to datetime in PythonUsing the dateutil.Relativedelta Object to Add Hours to datetime in PythonUsing the pandas.Date Offset() Function to Add Hours to datetime in PythonUsing the arrow.shift() […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.