如何从周数和周日C#获取日期

时间:2016-06-04 06:22:47

标签: c# asp.net date

如何从C#中的周数和周日数中获取日期。使用日历周规则。 例如,我有第43周和第3周的工作日。现在我想要日期。

提前致谢。

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

计算可以这样做:

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

在最后的选择周和工作日计算以确保一切正常。并且您可以尝试将DATEFIRST移动到星期一或星期日,并检查脚本是否返回正确的结果。

此外,您可以提取当前日期的年份数,并将周数添加到1月1日。 我的方法演示了如何在当前日期添加周数差异,并从当前日期删除“额外”工作日以获得所需的工作日。

您要测试的时间段介于week1_startweek2_end之间。

测试期间的另一种方法

你知道周数,你确切知道范围界限总是从第1周开始到第2周结束,所以

只需检查您要测试的日期的周数是否在给定的周数内

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

注意,如果年份不同,可能会失败。虽然最初的一周数字并不能说明它们属于哪一年。

答案 2 :(得分:2)

以下查询将为您提供一年中所有周的周一和周日

{
  "name": "moduleName",
  "engines": {
    "npm": "~2.4.12"
  }
}

结果将类似于以下示例:

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