简体   繁体   中英

How to get date from week number and week day C#

How can I get date from week number and week day number in C#. Using calendar week rule. For example I have week number 43 and week day number 3. Now I want the date.

Thanks In advance.

static DateTime GetDateFromWeekNumberAndDayOfWeek(int weekNumber, int dayOfWeek)
        {
            DateTime jan1 = new DateTime(2012, 1, 1);
            int daysOffset = DayOfWeek.Tuesday - jan1.DayOfWeek;

            DateTime firstMonday = jan1.AddDays(daysOffset);

            var cal = CultureInfo.CurrentCulture.Calendar;
            int firstWeek = cal.GetWeekOfYear(jan1, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

            var weekNum = weekNumber;
            if (firstWeek <= 1)
            {
                weekNum -= 1;
            }

            var result = firstMonday.AddDays(weekNum * 7 + dayOfWeek - 1);
            return result;
        }

http://forums.asp.net/t/1850105.aspx?Get+date+from+weeknumber

Calculation can be done this way:

declare @weeks table (week1 int, week2 int)
declare @today date = getdate()

select @@DATEFIRST

insert into @weeks(week1, week2)
values (12, 15)

;with computed_calendar as
(
  select
    @today today,
    datepart(week, @today) as current_week,
    datepart(weekday, @today) as current_weekday,

    w.week1,
    dateadd(week, w.week1-datepart(week, @today), @today) as week1_some_date,
    dateadd(day, 1-datepart(weekday, @today), dateadd(week, w.week1-datepart(week, @today), @today)) as week1_start,

    w.week2,
    dateadd(week, w.week2-datepart(week, @today), @today) as week2_some_date,
    dateadd(day, -datepart(weekday, @today), dateadd(week, w.week2+1-datepart(week, @today), @today)) as week2_end
  from @weeks w
)
select
  cc.today, cc.current_week, cc.current_weekday,

  cc.week1_start,
  datepart(week, cc.week1_start) week_1, 
  datename(weekday, cc.week1_start) week1_weekday,

  cc.week2_end,
  datepart(week, cc.week2_end) week_2,
  datename(weekday, cc.week2_end) week2_weekday
from computed_calendar cc

In the final select weeks and weekdays are calculated to ensure that everything is ok. And you may try moving DATEFIRST to monday or sunday and check whether script returns correct result or not.

Also you may do extract year number of current date and add weeks to January 01. My approach demonstrates how to add difference in weeks to current date and remove "additional" weekdays from current date to obtain required weekday.

The period you wanted to test is between week1_start and week2_end .

Another way to test period

You know week numbers and you know exactly that range bounds are always from the very start of week1 and the very end of week2, so

just check weeknumber of the date you want to test whether it is within given week numbers

select
... 
where datepart(week, my_date) between week1 and week2

Note, it may fail if year is different. Although initial week numbers don't tell which year they belong to.

Following query will give you Monday and Sunday of all weeks in a year

DECLARE @y int = 2016 
-- Find if it is leap year or not
DECLARE @leap int = IIF(DATEPART(dd,(EOMONTH(CONCAT(@y,'0201')))) = 29,1,0)
DECLARE @totalDays int = IIF(@leap = 1, 366, 365)

;WITH tally AS (
    -- select 365 or 366 (for leap year) days 
    SELECT top (@totalDays) name
      , ROW_NUMBER() OVER (ORDER BY object_id) Rn
    from sys.all_objects
)
, cc2 as (
    SELECT cast(DATEADD(d, Rn - 1, '2016-01-01') AS date) dayNo
    FROM tally
)
, cc3 as (
    SELECT dayNo, DATEPART(ww, dayNo) weekNo
    FROM cc2
)
, cc4 as (
    SELECT weekNo, min(dayNo) FirstDay
    FROM cc3
    GROUP BY weekNo
)
SELECT weekNo
  , cast(DATEADD(wk, DATEDIFF(wk, 0, FirstDay), 0) as date) Monday
  , cast(DATEADD(wk, DATEDIFF(wk, 0, FirstDay), 6) as date) Sunday
FROM cc4

Result will be something like below sample:

weekNo  Monday      Sunday
1       2015-12-28  2016-01-03
2       2016-01-04  2016-01-10
3       2016-01-11  2016-01-17
4       2016-01-18  2016-01-24
5       2016-01-25  2016-01-31
6       2016-02-01  2016-02-07
7       2016-02-08  2016-02-14
....
12      2016-03-14  2016-03-20
13      2016-03-21  2016-03-27
14      2016-03-28  2016-04-03
15      2016-04-04  2016-04-10
16      2016-04-11  2016-04-17
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM