How to Shift a Python Timestamp by One Day?

Estimated read time 1 min read

In Python, you can shift a timestamp by one day using the datetime module.

Here’s an example code that shifts a given timestamp by one day:

import datetime

# original timestamp
timestamp = datetime.datetime(2022, 3, 27, 12, 0, 0)

# shift timestamp by one day
shifted_timestamp = timestamp + datetime.timedelta(days=1)

print("Original timestamp:", timestamp)
print("Shifted timestamp:", shifted_timestamp)

In this example, we first define a datetime object representing the original timestamp. The datetime object is initialized with the year, month, day, hour, minute, and second of the timestamp.

To shift the timestamp by one day, we use the timedelta() function from the datetime module. The timedelta() function takes arguments to specify the number of days, seconds, microseconds, and/or other time units to shift the timestamp.

In this case, we pass days=1 as an argument to shift the timestamp by one day.

The result is a new datetime object representing the shifted timestamp. We can print both the original and shifted timestamps to verify the result.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply