获取本周的第一天和最后一天的日期知道周数 [英] Get date of first and last day of week knowing week number

查看:752
本文介绍了获取本周的第一天和最后一天的日期知道周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一周的第一天和最后一天的日期知道周数。



我得到一个开始日期和结束日期,代表在一年中选定的一周的第一天和最后一天。然后,我需要得到上年同周的开始日期和结束日期做一些数据的图形比较。



我设法根据给定的开始日期和结束日期的周数。现在,我需要得到的第一天,上年同周的最后一天的日期。 ?我怎么能做到这一点最快



编辑:这是我得到的周数:

 私人诠释GetWeekNumber(DateTime的日期)
{
的GregorianCalendar日历=新的GregorianCalendar(GregorianCalendarTypes.USEnglish);
返回calendar.GetWeekOfYear(日期,CalendarWeekRule.FirstDay,DayOfWeek.Sunday);
}


解决方案

您可以使用以下两种方法计算周数及一个根据给定一年定weeknumber的启动日期

  //该方法是从http://stackoverflow.com/a/11155102/284240 
借公共静态INT GetIso8601WeekOfYear(DateTime的时间)
{
DAYOFWEEK天= CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(时间);
如果(日> = DayOfWeek.Monday和放大器;和天< = DayOfWeek.Wednesday)
{
时间= time.AddDays(3);
}

返回CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(时间,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
}

公共静态的DateTime FirstDateOfWeek(年整型,诠释WEEKOFYEAR,System.Globalization.CultureInfo CI)
{
的DateTime jan1 =新日期时间(年,1, 1);
INT daysOffset =(INT)ci.DateTimeFormat.FirstDayOfWeek - (INT)jan1.DayOfWeek;
的DateTime firstWeekDay = jan1.AddDays(daysOffset);
INT firstWeek = ci.Calendar.GetWeekOfYear(jan1,ci.DateTimeFormat.CalendarWeekRule,ci.DateTimeFormat.FirstDayOfWeek);
如果((firstWeek&下; = 1 || firstWeek> = 52)及与放大器; daysOffset> = -3)
{
WEEKOFYEAR - = 1;
}
返回firstWeekDay.AddDays(WEEKOFYEAR * 7);
}



然后你就可以得到以下列方式两个日期:

  // 46 
INT thisWeekNumber = GetIso8601WeekOfYear(DateTime.Today);
// 2013年11月11日
的DateTime Firstdayofweek可= FirstDateOfWeek(2013年,thisWeekNumber,CultureInfo.CurrentCulture);
// 2012年11月12日
的DateTime firstDayOfLastYearWeek = FirstDateOfWeek(2012年,thisWeekNumber,CultureInfo.CurrentCulture);



...添加6天得到本周结束


I need to get the date of the first and last day of the week knowing the week number.

I get a start date and an end date, representing the first and last day of a selected week in a given year. then I need to get the start date and end date of the same week of the previous year to do a graphical comparison of some data.

I managed to get the week number based on the given start date and end date. Now I need to get the date of the first day and last day of the same week of the previous year. How could I do this quickest ?

EDIT: This is how I got the week number:

 private int GetWeekNumber(DateTime date)
    {
        GregorianCalendar calendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
        return calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);            
    }

解决方案

You can use following two methods to calculate the week-number and the start-date of a given weeknumber according to a given year:

// this method is borrowed from http://stackoverflow.com/a/11155102/284240
public static int GetIso8601WeekOfYear(DateTime time)
{
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
} 

public static DateTime FirstDateOfWeek(int year, int weekOfYear, System.Globalization.CultureInfo ci)
{
    DateTime jan1 = new DateTime(year, 1, 1);
    int daysOffset = (int)ci.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
    DateTime firstWeekDay = jan1.AddDays(daysOffset);
    int firstWeek = ci.Calendar.GetWeekOfYear(jan1, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
    if ((firstWeek <= 1 || firstWeek >= 52) && daysOffset >= -3)
    {
        weekOfYear -= 1;
    }
    return firstWeekDay.AddDays(weekOfYear * 7);
}

Then you can get both dates in the following way:

// 46
int thisWeekNumber = GetIso8601WeekOfYear(DateTime.Today); 
// 11/11/2013  
DateTime firstDayOfWeek= FirstDateOfWeek(2013, thisWeekNumber, CultureInfo.CurrentCulture); 
// 11/12/2012  
DateTime firstDayOfLastYearWeek = FirstDateOfWeek(2012, thisWeekNumber, CultureInfo.CurrentCulture);   

Add 6 days to get the end of the week.

这篇关于获取本周的第一天和最后一天的日期知道周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆