By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,658 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,658 IT Pros & Developers. It's quick & easy.

current week / weeks in year - best practice

P: n/a
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
Jul 31 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
Aljosa Mohorovic wrote:
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
Jul 31 '08 #2

P: n/a
On Jul 31, 3:58 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
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
Jul 31 '08 #3

P: n/a
On Jul 31, 5:42 pm, Aljosa Mohorovic <aljosa.mohoro...@gmail.com>
wrote:
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
Jul 31 '08 #4

P: n/a
Aljosa Mohorovic <al**************@gmail.comwrote:
>
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.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Aug 2 '08 #5

P: n/a
On Sat, Aug 02, 2008 at 07:46:49PM -0700, Dennis Lee Bieber wrote:
<heh>
What is the meaning of <heh?
e.
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
================================================== ======================
Aug 3 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.