473,738 Members | 6,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ye ar, 12, 31).strftime("% W"))
current_week = int(date(now.ye ar, now.month, now.day).strfti me("%W"))

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

Aljosa Mohorovic
Jul 31 '08 #1
5 6081
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.ye ar, 12, 31).strftime("% W"))
current_week = int(date(now.ye ar, now.month, now.day).strfti me("%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.w eb.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.strpti me("%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.comwro te:
>
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
3613
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 sure if it can be done, but all help welcomed. E.g. I have December and would like to see the last 2 weeks.. So this doesnt mean the last 15 days. What i mean by this is...
7
15149
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 attached), yet when I ran it for 28/09/2003 (UK date format) it returned Week 39. I would have expected it to return Week 40. However, I'm really stuck and my head is busting over this, so any pointers would be gratefully appreciated. Many thanks...
2
8626
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 convert(varchar(4),year(INVENTTRANS.DATEFINANCIAL)) + '-' + convert(varchar(2),month(INVENTTRANS.DATEFINANCIAL)) AS 'dfDate'
9
3419
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 period and 13 periods a year. normally 52 weeks per year but one 53-week a year every 6 years. The 53th week is included in period 13. Can someone advise any idea how to construct such conversion.
14
3495
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. However, the expectation changes - it may be 60% for a while, then change to 50%. Initially, I was averaging the expectation, along with the productivity, but what I'm being asked is to look at the average productivity/performance compared to...
10
1251
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 month of my choosing. Let me explain; I can get my week numbers no problem. I can get the date from the week numbers no problem. What I haven't been able to do is put things together and make a simple function that will allow my users to select from...
3
1844
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 no activity. I was planning to use ISO weeks because datetime has a convenient .isocalendar() method, but I want each output row to have a label like this: 2006 week 5 (Feb) However, to get the month (of the Thursday of that week) I have to
4
10829
by: Vince | last post by:
Given a week Number, how do I calculate the date that for the Monday of that week?
3
5836
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
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.