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.

Related Posts

  • 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() […]

  • Format Date to YYYYMMDD in Python
    03 March

    Format Date to YYYYMMDD in Python

    Table of ContentsDate formats in PythonHow to convert date to YYYYMMDD format in PythonUsing datetime.strftime() Function to Format Date to YYYYMMDD in PythonUsing datetime.date Object to Format Date to YYYYMMDD in PythonUsing date.isoformat() Function to Format Date to YYYYMMDD in PythonUsing arrow.format() Function to Format Date to YYYYMMDD in PythonUsing pendulum.to_date_string() Function to Format Date […]

  • Get today's date in Python
    03 March

    Get Today’s Date in Python

    Table of ContentsDate and Time objects in PythonGet Today’s Date in PythonUsing date.today() Function to Get Today’s Date in PythonUsing datetime.today() and datetime.strftime() Functions to Get Today’s Date in PythonUsing datetime.now() Function to Get Today’s Date in PythonUsing strftime() Function to Get Today’s Date in PythonUsing time.strftime() Function to Get Today’s Date in PythonUsing arrow.now() […]

  • Python get year from date
    23 January

    Get year from Date in Python

    Table of ContentsDates in PythonWays to get year from date stored in a datetime object in PythonUsing the year attributeUsing the strftime() functionUsing the pandas.to_datetime functionWays to get year from date stored as a string in PythonUsing the for loopUsing the re.search()Conclusion In this post, we will see how to get year from Date in […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.