I recently found myself needing to get the date of the first day of a particular week in C#. After browsing this rather confusing collection of answers, I pieced together parts of various code snippets into this little collection of utility methods.
The FirstDayOfWeek method is culture-sensitive, so in the en-GB culture the first day will be a Monday, whereas in the en-US culture it will be a Sunday. Given that, you can now easily get month and week boundaries for any given DateTime:
VB.NET Get the first last day of the Work Week
This formula returns a date that always represents the last day of the week, if the week ends on a Saturday. If you want the week to end on a different day of the week then the formula becomes more complex. If you want the week to end on a Sunday, then you simply need to add 1 to the formula:
Applying this same logic, you might think that you could figure out weeks ending on Friday by simply subtracting 1 from the formula. This is not the case since you are using the beginning of the year as your base. If you subtract 1, you run into the problem where January 1 of whatever year is a Saturday; if you subtract 1 you don't end up with the first Friday of the year but instead end up with December 31 of the previous year. To calculate for weeks ending on Friday you need a much more complex formula:
Thank you so much on your advise Catalin. It is very helpful.Indeed, I have another question. If I see the first week of the month differently. How could I change the formula to achieve this?For example, in Feb 2023, the first Monday of Feb should be 6/Feb/2023. But I want to see this as the second week, and the first week of Feb only have Wednesday to Sunday. So an empty string will return if I ask for the first Monday of Feb 2023. Plus, if I ask for the third Monday of Feb 2023, it should return me 13 Feb 2023.I want this algorithm as for every month and every year. So the first week of a month could be the last week of the previous month.
Thank you Catalin, your formula below can find the first week of those months (2/1, 1/5 and 2/10 only). Indeed, I want the second Monday of each months with the same algorithm. If I add +7 with your formula below. It will only return me 9/1 , 8/5 and 9/10 only. All others month became and empty cell. Please advise how to solve it?
To calculate the number of working days in a year, you can use the NETWORKDAYS function. NETWORKDAYS automatically excludes weekends and holidays if they are provided. In the example shown, the formula in E5 is:
To work with custom weekends (i.e. weekends are Sunday and Monday, etc.) switch to the more powerful NETWORKDAYS.INTL function, which allows control over which days of the week are considered workdays.
Returns the week in the year of the given date, based on the ISO 8601 week date calendar. Values range from 1-53 where the first week of the year is 1 and the last week of the year is 52 or 53, depending on the year.
Returns the week number in the year of the given date. Values range from 0-53 where the first week of the year is 0 and the last week of the year is 51, 52, or 53, depending on the year. The first and last weeks may not be a full seven days in length.
I use two diffrent mods, been using the SMOK Scar-18 that comes with the TFV9 Tank for proababily around 2-3 years, the coils that i used to use lasted a lot longer the V9 Mesh coils, those lasted anywhere from 2-3 weeks on high voltage and miss use (igniting with no juice in the tank) but i got savy and vaped around 60-75 wats (ohm resets to recomended detection on mod) after i dropped down to 55-64 and the coil would last around a month and a half. Keep in mind I vape heavily Now that those coils are harder to find and the smoke shop i buy them from sells the universal V8 baby beast mesh coils these have a LOT shoter life span. These coils are poorly made, some coming fresh out of the pack (I would gestimate about 1 out of every 20 coils) dont even screw on my mod and I have to take them back. For me I just smoked a 60 mil bottle in 3 and a half days these coils last around two weeks. But I am always vaping custard or desert flavors. The second mod was a gift from a friend, it is a SMOK NORD 4. The coils I am using are the RPM 2 Mesh coils. These coils were only lasting me around 2 weeks but now they seem to last around a month or so (I did turn down the wattage) When i was desprate I used to save my old coils and put them in a pan and boil them for about a half hour over the stove and agitated them by shaking them in the hot water using tongs. This added an additional two weeks on the coils which was cool though they had some resigual flavor on them and some coils were so dirty i had to get a cue-tip and scrub the actual coil part because there was burnt on crud on the coils. I dont recommend this because it sometimes leaves a stain ring around your pot which is hard to get off. But its a cool concept you get to see all the nasty stuff that comes out at the bottom of the pan! Hope this is useful to all of you. And yes I can confirm that smoking desert/custard flavors compared to say a Licorice flavor that would last me twice as long on the same settings and coil.
Another frequent operation when dealing with dates is calculating the number of weeks that have passed since the beginning of the year. First we should clarify what week should be considered as the first week of a year. According to the ISO 8601 standard, the first week of a year is a week with the majority (four or more) of days in the starting year. In our case it is better to use another equivalent definition of the first week: the week with the date, January, 4th.
Optional. It is a value that specifies the first day of the week. If this parameter is omitted, the Format function assumes that Sunday is the first day of the week. This parameter can be one of the following values:
Optional. It is a value that specifies the first week of the year. If this parameter is omitted, the Format function assumes that the week that contains January 1 is the first week of the year. This parameter can be one of the following values:
Outstanding work Daniel! I am not sure what I am doing wrong but the code below seems to work, but its having a fault. I am looking for every other Tuesday starting with 7/1/2014. The following seems to work, except it leaves out the 6/30/2015 interval and is then subsequently off by one week. This seems to happen near or around the 1 year mark from the starting date. I cant figure out of its something I am doing wrong, or in the procedure itself. 2ff7e9595c
Comments