Suggestion:yesterday() in datetime()

I suggest add datetime.yesterday() like datetime.now(), such as datetime.yesterday().date(), even it is easy to code using timedelta(days=1), but it’s easy to understand while analysis daily data backward.

Just as a note: If this would be added, it should be added as date.yesterday(), similar to date.today(). (And for completeness sake, date.tomorrow() would makes sense as well.)

5 Likes

Thanks for your addition, I thought of this after Posting, and indeed Date() is more appropriate (my own project only calls datetime() because it involves hour and minute data, so I am lazy to ‘import date’ when I occasionally need the previous day’s date data, So I used ‘datetime.now().date()’ instead)

Somewhat related discussion: datetime.datetime.today makes no sense and should be removed · Issue #86070 · python/cpython · GitHub

Even if this were added as date.yesterday(), we’d still inevitably get a datetime.yesterday() since datetime is a subclass of date.

If accepted, I imagine this won’t be available until 3.13 at the earliest… yesterday seems so far away.

6 Likes

As I known, date is subclass of datetime…

U r right, Thanks

Subclass relationships:

object

  • timedelta
  • tzinfo
    • timezone
  • time
  • date
    • datetime

I think yesterday(), tomorrow() and the like are too niche to add to the datetime module. And they’re trivial to implement, and hard to get wrong.

6 Likes

Maybe we could implement date + int and date - int. Then we could write

    date.today() - 1  # yesterday
    date.today() + 2  # the day after tomorrow
    date.today() + 7  # same day next week

A lot easier than

    date.today() + date.timedelta(days=2)

It’s easier but a heck of a lot less specific and obvious. Units are good!

3 Likes

Also a lot less precise and possibly surprising. Why should the int mean days?

2 Likes

If you’re just looking for a nicer syntax than timedelta then you can do:

days = timedelta(days=1)

tomorrow = date.today() + 1*days
next_week = date.today() + 7*days
6 Likes

Like Oscar pointed out, a simple recipe is to just define DAY as a constant.

>>> from datetime import date, timedelta
>>> DAY = timedelta(days=1)
>>> date.today()
datetime.date(2023, 12, 20)
>>> date.today() - DAY
datetime.date(2023, 12, 19)
>>> date.today() - 2 * DAY
datetime.date(2023, 12, 18)
>>> date.today() + 31 * DAY
datetime.date(2024, 1, 20)

You could even define a custom function or lambda to take make a tomorrow callable:

>>> tomorrow = lambda: date.today() + DAY
>>> tomorrow()
datetime.date(2023, 12, 21)

You could similarly define a WEEK as so:

>>> WEEK = timedelta(days=7)
>>> date.today()
datetime.date(2023, 12, 20)
>>> date.today() + WEEK
datetime.date(2023, 12, 27)

You wouldn’t want to do any more than a week, as months and years change based on the weirdness of the calendar.

2 Likes

day is the atomic unit of a date

2 Likes

Personally, I just suggest it is easy to understand & define. specially using in loop. such as below:

for …(date)…
date = date.yesterday()

or

if date_from_db == date.yesterday() :

if date_from_db == date.today():

if date_from_db == date.tomorrow():

or
if date.today():
analysis(date.yesterday())

That’s actually such a neat trick that I wish that datetime would add those constants. While it’s easy to define them yourself, that would great for discoverability and ease of use.

The problem with adding it to the stdlib is that you really have to start looking at corner cases, such as how to interact with Daylight Savings Time. There’s every chance that corner cases might be missed if it were added to the stdlib, and then we’d be stuck with the implementation and still have to work around it with custom code or external libraries, which leaves us in no better spot than today.

See: python - How to subtract a day from a date? - Stack Overflow

2 Likes