Anyone know an easy way to get the date of the first day in the week (monday here in Europe). I know the year and the week number? I'm going to do this in C#.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
this is my solution when we want to calculate a date given year, week number and day of the week.
I used one of the solutions but it gave me wrong results, simply because it counts Sunday as a first day of the week.
I changed:
to:
and now it is working as a charm.
The free Time Period Library for .NET includes the ISO 8601 conform class Week:
Good news! A pull request adding
System.Globalization.ISOWeek
to .NET Core was just merged and is currently slated for the 3.0 release. Hopefully it will propagate to the other .NET platforms in a not-too-distant future.You should be able to use the
ISOWeek.ToDateTime(int year, int week, DayOfWeek dayOfWeek)
method to calculate this.You can find the source code here.
Assuming the week number starts at 1
This should give you the first day in any given week. I haven't done a lot of testing on it, but looks like it works. It's smaller solution than most other's I found on the web, so wanted to share.