How do I subtract a day from a datetime object?

Asked 2 years ago, Updated 2 years ago, 81 views

How do I subtract a day from a datetime.datetime object?

For example, 2016-01-21 15:36:43.058249 -> 2016-01-20 15:36:43.058249 You have to take out a day like this.

python datetime date

2022-09-22 11:46

1 Answers

datetime.datetimeIf you are operating on an object, you usually write datetime.timedelta()

The circular form of the datetime.timedelta() function is datetime.timedelta([days[,seconds[,microsconds[,milliseconds[,minutes[,hours[,weeks]]]]]]]]) The default value for all factors is 0, and you can receive int, long, and float regardless of positive or negative.

Here's how to write:

from datetime import datetime, timedelta

d = datetime.today() - timedelta(days=1)


2022-09-22 11:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.