As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
If someone were to say something happens in the first or last week of some month, how would you define that span?
Background
I'm working on a library that needs to handle this kind of thing in an intuitive way, but my intuition doesn't claim one case or the other.
I can make it select different behavior but still need a good default.
For a concrete example I'll just to pick an random month: July 2008
s m t w r f s
--------------------
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
(Note: I'll assume the week starts with Sunday but the question could be asked re Monday as well)
What span is the first week of the month?
June 29 - July 5 -- the week that includes the first
July 6 - 12 -- the first week that is all the way into the month
The second week is just the 7 days following the first right?
What span is the last week of the month?
July 27 - Aug 2 -- ditto but the last day
July 20 - 26 -- ditto but the last week
Non wiki comments welcome
There is no answer because it varies even inside a country.
However, this source seems to think it's defined by the ISO standard as the first week that contains a thursday. However the above source says that the ISO standard does not list a specification for 'first week of the month'
More than 300 chars - sorry!
The ISO 8601 standard defines the first week of the YEAR as:
calendar week number
ordinal number which identifies a calendar week within its calendar year according to the rule that the first
calendar week of a year is that one which includes the first Thursday of that year and that the last calendar
week of a calendar year is the week immediately preceding the first calendar week of the next calendar year
There is nothing in ISO 8601 about the first week of the month because, as has been noted, the definition varies by country (and even by company, for example). Oh, and the standard says that weeks start on Monday.
If you are building a library, you should allow the user to choose how they want to handle it either by having methods that return the first/last full/partial week of the month or by passing an argument that indicates whether you want the full/partial week. Clearly document whichever is the default if you provide methods that don't require the specification.
public Week GetFirstFullWeekOfMonth( DateTime forDate ) { }
public Week GetFirstPartialWeekOfMonth( DateTime forDate ) { }
Or
public Week GetFirstWeekOfMonth( DateTime forDate, bool includePartial ) {}
The first week of the month is the first week that is all the way int the month
July 6 - 12
The first week of the month is the week that includes the first
June 29 - July 5
The last week of the month is the week that includes the last day
July 27 - Aug 2
The last week of the month is the last week that is all the way int the month
July 20 - 26
My definition would be the first week is the week of the first working day in the month,
and likewise the last week is the week of the last working day in the month.
The first week of the month is the first week that owns 4 or more days of the defined week period. (Not really but I thought I'd throw it in as an option)
The first week of the month is week 0 - the week that has the first day of the month.
The last week is the week with the last day of the month
Hence the weeknumber of the last week of july IS THE SAME as the weeknumber of the first week of august
** UNLESS **
the month in question ends on the last day of the last week, in wich case the following month would start on the first day of the new week and have a different weeknumber
:)
There's a fairly obvious option you are missing. The first week of the month is the seven days starting with the first day of the month. Hence, in your example, the first week would be Tuesday, the 1st through Monday, the 7th. This way, the first through 4th weeks would always be fully contained within the month, and only the 5th week would cross over.
Talk to your users and ask them what they think it means when the program says to them "First week of the month" and "Last week of the month".
Related
I have the below columns
StartDate EndDate CountDay
01 May 20 05 May 20 ?
As you see, 01 May is Friday, so from 01-05 May if we count all days including weekend it will be 4 days.
What I want is on column "CountDay" it only counts the Workdays, not the weekend.
SO the expected result would be 2.
Anyone know how to do it using a formula in Google Sheets?
Do you consider Fridays as part of the weekend?
If yes, then you could also try the following formula:
=NETWORKDAYS.INTL(A10, B10,"0000111")
If not, please use this formula:
=NETWORKDAYS.INTL(A10, B10)
How the formulas work.
By using the function NETWORKDAYS.INTL we can "adjust" the weekend (non-working weekdays) to our liking.
In this case we account Fridays as our non-working weekdays by using as the 3rd parameter 0000111 instead of the default 0000011 where every 0 represents a working weekday and every 1 a non-working weekday.
(Very useful for people working part-time)
Someone who has part-time work on only Mondays, Wednesdays and Fridays and wants to calculate the working days Friday, 1 May 2020 - Tuesday, 30 June 2020 could adjust the formula to:
=NETWORKDAYS.INTL(A10, B10,"0101011")
As explained on the official Google help page for NETWORKDAYS.INTL
weekend – [ OPTIONAL – 1 by default ] – A number or string representing which days of the week are considered weekends.
String method: Weekends can be specified using seven 0s and 1s, where the first number in the set represents Monday and the last number is for Sunday. A zero means that the day is a work day, a 1 means that the day is a weekend. For example, “0000011” would mean Saturday and Sunday are weekends.
Number method: Instead of using the string method above, a single number can be used. 1 = Saturday/Sunday are weekends, 2 = Sunday/Monday and this pattern repeats until 7 = Friday/Saturday. 11 = Sunday is the only weekend day, 12 = Monday is the only weekend day and this pattern repeats until 17 = Saturday is the only weekend day.
I just found how to do it:
=if(weeknum(A10)<weeknum(B10),B10-A10-2*(weeknum(B10)-weeknum(A10)),B10-A10)
something like that
I am trying to get week numbers ( resetting at 1 for each month) as per ISO format for each month in 2019.For example I am interested in getting
All dates in July 2019: week 1 to 4,
All dates in Aug 2019 : week 1 to 4 and so on.
I first created the calculated field (Week_Number_ISO) to get the overall week number in year 2019.I used the following formula;
DATEPART('iso-week',[ Date]) which works as intended.
To get the monthly week number I used the following formula
INT((DATEPART('day',[Created Date])-DATEPART('iso-weekday',[Created Date])+7)/7)+1.
(Idea was to calculate the date of the first day of each week & then divide by 7 and take the integer part)
As per the ISO format, shouldn't July 29 to 31st be a part of week 4 for July?But the formula is showing it as week 5 for July 2019.I feel I am missing something in the formula or am missing something about ISO week number resetting at 1 for each month.
Can someone help me?
Here is an example of the dates in July 2019 and the associated week numbers.
Why would July 28th-July 31st 2019 be considered week 4?
What iCal RRULE expression should I use to find a Friday after last Wednesday of month? So I need (RRULE:FREQ=MONTHLY;BYDAY=-1WE) + 2 days...
I agree with Evert that this is not possible with a single RRULE.
You can, however, achieve that with a combination of multiple RRULEs.
That way you can split your event into multiple events each one having one of the RRULEs below:
If Wednesday falls on the last or second last day of a month, our Friday will be the 1st or 2nd of the next month
FREQ=MONTHLY;BYMONTHDAY=1,2;BYDAY=1FR
In months with 31 days the earliest date for the last Wednesday is the 25th, so the next Friday will be one of day 27-31:
FREQ=MONTHLY;BYMONTH=1,3,5,7,8,10,12;BYMONTHDAY=27,28,29,30,31;BYDAY=FR
In months with 30 days the earliest date for the last Wednesday is the 24th, so the next Friday will be one of day 26-30:
FREQ=MONTHLY;BYMONTH=4,6,9,11;BYMONTHDAY=26,27,28,29,30;BYDAY=FR
February is a tricky one, because in non-leap years the the Friday we want will be between the 24th and 28th, but in leap years it's between the 25th and 29th. However, within the next 50 years there is only one leap year in which February 24th is a Friday, which is 2040 (check out the result of FREQ=MONTHLY;BYMONTHDAY=24;BYMONTH=2;BYDAY=FR), so the following RRULE will be correct until 2040:
FREQ=MONTHLY;BYMONTH=2;BYMONTHDAY=24,25,26,27,28,29;BYDAY=FR
If you're concerned about the leap years, add an EXDATE like below and you're good for the next 100 years (make these DATE-TIME values if your start date has a time):
EXDATE;VALUE=DATE:20400224,20680224,20960224,21080224
If you know for sure that your clients support multiple RRULEs (which has been deprecated halfheartedly in RFC 5545 but still was perfectly valid in RFC 2445) you could add them into a single event as well, but I wouldn't recommend that.
Would it be possible to get same day of week last year using Excel? please below example:
Input: Monday 9 Nov 2015 | Output: 10 Nov 2014
Thanks
Simply subtract 52 full weeks with 7 days = 364 days. So if the date is in A1, the formula =A1-364 will get the date exactly 52 weeks before, which is the same day of week in the year before.
To show that it works even for leap years, try the following:
You see the formula date - 364 (=A2-364, =A3-364, ...) always gets the same day of week a year before. That is because it gets the day minus 52 full weeks (52 * 7 days) before. In leap years it gets a different day but the same day of week.
Try this:
=DATE(YEAR(A1)-1,MONTH(A1),DAY(A1))+WEEKDAY(A1)-WEEKDAY(DATE(YEAR(A1)-1,MONTH(A1),DAY(A1)))
It returns the closest date within a week. A1 is the cell with this year's date.
I'm trying to separate weeks from timestamp per quarter so it should be between 1-13 week per quarter so I used function week() but it takes between 1-52 week as whole year so I made it to be divided by function of quarter like below
select Week (EVENTTIMESTAMP) / QUARTER (EVENTTIMESTAMP) from KAP
The thing here that results aren't accurate; for example it shows:
time stamp 2014-07-06 12:13:03.018
week number 9
which isn't correct because July is first month in Q3 and it's in the 6 days so it should be 1 week from Q3 not 9.
Any suggestion where it go wrong?
You want something like WEEK modulo 13 to get week number within a quarter. You will have to tinker with 'modulo 13 yields 0..12' by adding or subtracting one at appropriate points.
Some minimal Google searching using 'ibm db2 sql modulo' yields DB2 MOD function:
The MOD function divides the first argument by the second argument and returns the remainder.
Hence MOD(WEEK(...), 13), except you probably need MOD(WEEK(...)-1, 13) + 1, as intimated already.
You may need to watch for what the WEEK() function does at year ends:
The WEEK function returns an integer in the range of 1 to 54 that represents the week of the year. The week starts with Sunday, and January 1 is always in the first week.
I'm curious about how they can come up with week 54. I suppose it requires 1st January to be a Saturday (so 2nd January is the start of week 2) of a leap year, as in 2000 and 2028. Note that week 53 and (occasionally) week 54 will show up as weeks 1 and 2 of Q5 unless you do something. Also, Saturday 2000-03-25 would be the end of Q1 and Sunday 2000-03-26 would be the start of Q2 under the regime imposed by the WEEK() function and a simple MOD(WEEK(...), 13) calculation. You're likely to have to tune this to meet your real requirements.
There's also the WEEK_ISO() function:
The WEEK_ISO function returns an integer in the range of 1 to 53 that represents the week of the year. The week starts with Monday and includes seven days. Week 1 is the first week of the year that contains a Thursday, which is equivalent to the first week that contains January 4.
Note that under the ISO scheme, the 3rd of January can be in week 52 or 53 of the previous year, and the 29th of December can be in week 1 of the next year. Curiously, there doesn't seem to be a YEAR_ISO() function to resolve such ambiguities.
In a data warehouse, the proper solution to this is to create a time dimension that contains static mappings for days/weeks/months/quarters/years. This provides the ability to define these based on your business' fiscal calendar (if it is not following on the calendar year).
See: http://www.kimballgroup.com/1997/07/10/its-time-for-time/ for more information.