current week / weeks in year - best practice

A

Aljosa Mohorovic

i use this to find out current week and total number of weeks for
current year:
now = datetime.now()
weeks_in_year = int(date(now.year, 12, 31).strftime("%W"))
current_week = int(date(now.year, now.month, now.day).strftime("%W"))

is this the best way or is there a better way?

Aljosa Mohorovic
 
D

Diez B. Roggisch

Aljosa said:
i use this to find out current week and total number of weeks for
current year:
now = datetime.now()
weeks_in_year = int(date(now.year, 12, 31).strftime("%W"))
current_week = int(date(now.year, now.month, now.day).strftime("%W"))

is this the best way or is there a better way?

Instead of datetime.now() use date.today(), which removes a lot of
boilerplate.

int(date.today().strftime("%W"))

Apart from that, I think it's the way to go.

Diez
 
A

Aljosa Mohorovic

Instead of datetime.now() use date.today(), which removes a lot of
boilerplate.

int(date.today().strftime("%W"))

Apart from that, I think it's the way to go.

what if i know current context week = 20 (example), what would be the
best way to get datetime objects for first and last day of current
context week?
by "current context week" i don't mean current week for current year
but current week when program is iterating all weeks in year.

Aljosa
 
A

Aljosa Mohorovic

what if i know current context week = 20 (example), what would be the
best way to get datetime objects for first and last day of current
context week?
by "current context week" i don't mean current week for current year
but current week when program is iterating all weeks in year.

if w = current context week and now is current datetime object this
is how i calculate days:
first_day = datetime.strptime("%s %s" % (now.year, str((w-1)*7)), "%Y
%j")
last_day = first_day + timedelta(days=6)

any comments on this?

Aljosa Mohorovic
 
T

Tim Roberts

Aljosa Mohorovic said:
what if i know current context week = 20 (example), what would be the
best way to get datetime objects for first and last day of current
context week?
by "current context week" i don't mean current week for current year
but current week when program is iterating all weeks in year.

One of the problems is that "current week of the year" is not a
well-defined term. Some companies define the first week as the week that
contains January 1. Some companies define the first week as the first full
week, so that a partial Jan 1 week is actually part of the previous year.
And you get the whole "does the week start on Sunday or Monday" debate as
well.

You need to make sure that the datetime functions match your business
rules.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top