问题
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
回答1:
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.
回答2:
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 ) {}
回答3:
The first week of the month is the first week that is all the way int the month
- July 6 - 12
回答4:
The first week of the month is the week that includes the first
- June 29 - July 5
回答5:
The last week of the month is the week that includes the last day
- July 27 - Aug 2
回答6:
The last week of the month is the last week that is all the way int the month
- July 20 - 26
回答7:
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.
回答8:
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
:)
回答9:
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)
回答10:
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.
回答11:
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".
来源:https://stackoverflow.com/questions/604031/what-is-the-first-last-week-of-a-month