VPfB
VPfB

Reputation: 17247

Is there a way to use datetime.date without a year?

The answer is probably no, but I hope you don't mind if I ask just to be sure. Can datetime.date objects be somehow used like this?

import datetime as dt
is_summer = dt.date(month=7, day=1) <= dt.date.today() < dt.date(month=8, day=31)  # wrong!

Currently, I'm transforming the datetime.date objects to (month, day) tuples wherever I mean "every year". I'm considering to completely abandon datetime and use just the tuples everywhere.

Upvotes: 1

Views: 3009

Answers (1)

Alex Hall
Alex Hall

Reputation: 36013

Your current method sounds pretty good. You could also do:

def date_this_year(**kwargs):
    return dt.date(year=dt.date.today().year, **kwargs)

is_summer = date_this_year(month=7, day=1) <= dt.date.today() < date_this_year(month=8, day=31)

Upvotes: 1

Related Questions