472,982 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

current week / weeks in year - best practice

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
5 6026
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not...
7
by: Shuffs | last post by:
Could someone, anyone please tell me what I need to amend, to get this function to take Sunday as the first day of the week? I amended the Weekday parts to vbSunday (in my code, not the code...
2
by: Papegoja | last post by:
Hi, A week ago I had a question how to get MS Query to return a standard date to a "YEAR-MONTH" (YYYY-MM) format. I received a great answer from Jerry Boone!!! It follows: SELECT...
9
by: Ray | last post by:
I need to convert the normal calendar to show the week no., the period no. and the financial year. The financial year format is as follows:- Date start: 2 May, 2005 7 days a week, 4 weeks a...
14
by: Tina | last post by:
My employer tracks productivity/performance of clinicians (how much they bill) each week, its averages for the month, and the 6 months. These averages are compared to their expected productivity....
10
by: Jim | last post by:
I'm sure this has been asked before but I can't find any postings. I have a table that has weekly inspections for multiple buildings. What I need to do is break these down by the week of the...
3
by: Mike Orr | last post by:
What's the best way to summarize data by week? I have a set of timestamped records, and I want a report with one row for each week in the time period, including zero rows if there are weeks with...
4
by: Vince | last post by:
Given a week Number, how do I calculate the date that for the Monday of that week?
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, is there a way if you are given a Year and a Week an easy way to go back say 26 weeks ago from given year/week. for example, given: 2008/16 26 weeks prior is: 2007/43 thanks,
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.