isnberg 70
isnberg 70

Reputation: 7

How to check whether a date is in the next week, python

Basically, I'm trying to check whether a date, e.g. 2021-07-08, is in the next week, or the week after that, or neither.

#I can call the start and end dates of the current week

start = tday - timedelta(days=tday.weekday())
end = start + timedelta(days=6)
print("Today: " + str(tday))
print("Start: " + str(start))
print("End: " + str(end))

# and I can get the current week number. 

curr_week = datetime.date.today().strftime("%V")
print(curr_week)

Is there a better way than getting a list of dates in curr_week + 1 and then checking whether date is in in that list? Thanks so much

Upvotes: 0

Views: 1960

Answers (4)

user1706918
user1706918

Reputation:

[see Alfred's answer]

You can get the week number directly as an integer integer from the IsoCalendarDate representation of each date.

from datetime import datetime
date_format = '%Y-%m-%d'
t_now = datetime.strptime('2021-08-11', date_format)
target_date = datetime.strptime('2021-08-18', date_format)

Upvotes: 1

Alfred Rodenboog
Alfred Rodenboog

Reputation: 403

GENERAL ANSWER

It is best to stick to datetime and timedelta, since this handles all edge cases like year changes, years with 53 weeks etc.

So find the number of the next week, and compare the weeknumber of the week you want to check against that.

import datetime

# Date to check in date format:
check_date = datetime.datetime.strptime("2021-09-08", "%Y-%d-%m").date()

# Current week number:
curr_week = datetime.date.today().strftime("%V")
# number of next week
next_week = (datetime.date.today()+datetime.timedelta(weeks=1)).strftime("%V")
# number of the week after that
week_after_next_week = (datetime.date.today()+datetime.timedelta(weeks=2)).strftime("%V")


# Compare week numbers of next weeks to the week number of the date to check:
if next_week == check_date.strftime("%V"):
    # Date is within next week, put code here
    pass
elif week_after_next_week == check_date.strftime("%V"):
    # Date is the week after next week, put code here
    pass

OLD ANSWER

This messes up around year changes, and modulo doesn't fix it because there are years with 53 weeks.

You can compare the week numbers by converting them to integers. You don't need to create a list of all dates within the next week.

import datetime

# Date to check in date format:
check_date = datetime.datetime.strptime("2021-07-08", "%Y-%d-%m").date()

# Current week number, make it modulo so that the last week is week 0:
curr_week = int(datetime.date.today().strftime("%V"))

# Compare week numbers:
if curr_week == (int(check_date.strftime("%V"))-1):
    # Date is within next week, put code here
    pass
elif curr_week == (int(check_date.strftime("%V"))-2):
    # Date is the week after next week, put code here
    pass

Upvotes: 2

Jason Pan
Jason Pan

Reputation: 792

Just using datetime comparing:

from datetime import datetime, timedelta


def in_next_week(date):
    """ -1: before; 0: in; 1: after next week;"""
    today = datetime.today()
    this_monday = today.date() - timedelta(today.weekday())
    start = this_monday + timedelta(weeks=1)
    end = this_monday + timedelta(weeks=2)
    return -1 if date < start else 0 if date < end else 1

Test cases:

for i in range(14):
    dt = datetime.today().date() + timedelta(days=i)
    print(dt, in_next_week(dt))

Upvotes: 0

klegoff
klegoff

Reputation: 51

You can cast the date you want to check in datetime, and then compare the week numbers.

# date you want to check
date = datetime.datetime.strptime("2021-07-08","%Y-%m-%d")

# current date
tday = datetime.date.today()

# compare the weeks
print(date.strftime("%V"))
print(tday.strftime("%V"))

27
32

Upvotes: 1

Related Questions