获取知道周数的第一天和最后一天的日期

时间:2013-11-11 08:15:10

标签: c# date

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

我得到一个开始日期和结束日期,代表给定年份中选定周的第一天和最后一天。然后我需要获取上一年同一周的开始日期和结束日期,以对某些数据进行图形比较。

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

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

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

8 个答案:

答案 0 :(得分:19)

您可以使用以下两种方法根据给定年份计算给定周数的周数和开始日期:

// 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);
}

然后您可以通过以下方式获取两个日期:

// 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);   

添加6天以结束本周结束。

答案 1 :(得分:12)

这是我的解决方案:

public static DateTime FirstDayOfWeek(DateTime date)
{
    DayOfWeek fdow = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
    int offset = fdow - date.DayOfWeek;
    DateTime fdowDate = date.AddDays(offset);
    return fdowDate;
}

public static DateTime LastDayOfWeek(DateTime date)
{
    DateTime ldowDate = FirstDayOfWeek(date).AddDays(6);
    return ldowDate;
}

答案 2 :(得分:2)

上述方法计算2016年第1周的第一个日期错误。

基于先前答案的固定代码

 public DateTime FirstDateOfWeek(
        int year,
        int weekOfYear)
    {
        var newYear = new DateTime(year, 1, 1);
        var weekNumber = newYear.GetIso8601WeekOfYear(); 

        DateTime firstWeekDate;

        if (weekNumber != 1)
        {
            var dayNumber = (int) newYear.DayOfWeek;
            firstWeekDate = newYear.AddDays(7 - dayNumber + 1);
        }
        else
        {
            var dayNumber = (int)newYear.DayOfWeek;
            firstWeekDate = newYear.AddDays(-dayNumber + 1);
        }

        if (weekOfYear == 1)
        {
            return firstWeekDate;
        }

        return firstWeekDate.AddDays(7 * (weekOfYear - 1));
    }

Plus test

    [TestCase(2016, 1, 2016, 1, 4)]
    [TestCase(2016, 2, 2016, 1, 11)]
    [TestCase(2016, 52, 2016, 12, 26)]
    [TestCase(2014, 1, 2013, 12, 30)]
    public void FirstDateOfWeek_ShouldReturnCorrectDay(int year, int weekNumber, int correctYear, int correctMonth, int correctDay)
    {
        var helper = new DateHelper();
        var result = helper.FirstDateOfWeek(year, weekNumber);

        Assert.That(result.Year, Is.EqualTo(correctYear));
        Assert.That(result.Month, Is.EqualTo(correctMonth));
        Assert.That(result.Day, Is.EqualTo(correctDay));
    }

答案 3 :(得分:0)

从原始DateTime中减去1年,然后再次调用GetWeekNumber方法。

TimeSpan oneYear = dateTime.AddYears(1) - dateTime;
var lastYearWeekNumber = GetWeekNumber(dateTime.Subtract(oneYear));

答案 4 :(得分:0)

    private static DateTime FirstDateOfWeek(int year, int weekOfYear)
    {
        var firstDate = new DateTime(year, 1, 4);
        //first thursday of the week defines the first week (https://en.wikipedia.org/wiki/ISO_8601)
        //Wiki: the 4th of january is always in the first week
        while (firstDate.DayOfWeek != DayOfWeek.Monday)
            firstDate = firstDate.AddDays(-1);

        return firstDate.AddDays((weekOfYear - 1)*7);
    }

更简单,更高效的解决方案!

答案 5 :(得分:0)

您可以找到有关如何获取当前最后第一最后日期的解决方案根据传递的参数日期的星期数,现在您还可以获得前一年同一周的第一天最后一天的日期>。

您还可以更改某些设置来更改您的第一个工作日,以便根据需要获得结果。

假设您的工作日从星期一(星期日除外)开始,那么您需要做的就是更改切换条件并继续获得所需的结果。

public DateTime GetFirstDateOfCurrentWeek(DateTime dtDate)
{
 //In this function we assumed that week start from sunday and end on saturday
    int no = 0;
    switch (dtDate.DayOfWeek.ToString())
    {
        case "Sunday":
            no = 0;
            break;
        case "Monday":
            no = 1;
            break;
        case "Tuesday":
            no = 2;
            break;
        case "Wednesday":
            no = 3;
            break;
        case "Thursday":
            no = 4;
            break;
        case "Friday":
            no = 5;
            break;
        case "Saturday":
            no = 6;
            break;
        default:
            no = 0;
            break;
    }
    return dtDate.AddDays(-no);
 }

public DateTime GetLastDateOfCurrentWeek(DateTime dtDate)
{
    int no = 0;
    switch (dtDate.DayOfWeek.ToString())
    {
        case "Saturday":
            no = 0;
            break;
        case "Friday":
            no = 1;
            break;
        case "Thursday":
            no = 2;
            break;
        case "Wednesday":
            no = 3;
            break;
        case "Tuesday":
            no = 4;
            break;
        case "Monday":
            no = 5;
            break;
        case "Sunday":
            no = 6;
            break;
        default:
            no = 0;
            break;
    }
    return dtDate.AddDays(no);
}

public DateTime GetFirstDateOfLastWeek(DateTime dtDate)
{
    return GetFirstDateOfCurrentWeek(dtDate).AddDays(-7);
}

public DateTime GetLastDateOfLastWeek(DateTime dtDate)
{
    return GetLastDateOfCurrentWeek(dtDate).AddDays(-7);
}

public DateTime GetFirstDateOfCurrentWeekOfPrvYear(DateTime dtDate)
{
    int dayOfYear = dtDate.DayOfYear;
    DateTime firstDateOfThisYear = (Convert.ToDateTime("01/01/" + dtDate.Year.ToString()));
    DateTime firstDateofPrvYear = (firstDateOfThisYear.AddYears(-1));
    DateTime dateOfPrvYear = (firstDateofPrvYear.AddDays(dayOfYear));

    return GetFirstDateOfCurrentWeek(dateOfPrvYear);
}

public DateTime GetLastDateOfCurrentWeekOfPrvYear(DateTime dtDate)
{
    int dayOfYear = dtDate.DayOfYear;
    DateTime firstDateOfThisYear = Convert.ToDateTime("01/01/" + dtDate.Year.ToString());
    DateTime firstDateofPrvYear = (firstDateOfThisYear.AddYears(-1));
    DateTime dateOfPrvYear = (firstDateofPrvYear.AddDays(dayOfYear));

    return GetLastDateOfCurrentWeek(dateOfPrvYear);
}

DateTime dt = Convert.ToDateTime(“ 12/12/2019”);

int dayOfYear = dt.DayOfYear;
string weekOfYear = (dt.DayOfYear / 7).ToString();
string firstDateOfThisYear = (Convert.ToDateTime("01/01/" + dt.Year.ToString())).ToShortDateString();
string firstDateofPrvYear = (Convert.ToDateTime(firstDateOfThisYear).AddYears(-1)).ToShortDateString();
string dateOfPrvYear = (Convert.ToDateTime(firstDateofPrvYear).AddDays(dayOfYear)).ToShortDateString();
string firstDateofWeekPrvYear = GetFirstDateOfCurrentWeek(Convert.ToDateTime(dateOfPrvYear)).ToShortDateString();
string lastDateofWeekPrvYear = GetLastDateOfCurrentWeek(Convert.ToDateTime(dateOfPrvYear)).ToShortDateString();
string weekOfPrvYear = (Convert.ToDateTime(dateOfPrvYear).DayOfYear / 7).ToString();

MessageBox.Show("Day of Year: " + dayOfYear.ToString());
MessageBox.Show("Week of Year: " + weekOfYear);
MessageBox.Show("First day of this year: " + firstDateOfThisYear);
MessageBox.Show("First day of prv year: " + firstDateofPrvYear);
MessageBox.Show("Date of Prv Year: " + dateOfPrvYear);
MessageBox.Show("First Date of Week Prv Year: " + firstDateofWeekPrvYear);
MessageBox.Show("Last Date of Week Prv Year: " + lastDateofWeekPrvYear);
MessageBox.Show("Day of Prv Year: " + weekOfPrvYear.ToString());

答案 6 :(得分:-1)

由于每周有7天,您将知道第10周与第71天至第77天相同。您知道每月的天数(不要忘记闰年)。构建确切的日期然后,您可以获得有关那些日子的信息。

答案 7 :(得分:-1)

Tim Schmelter的方法不适用于周一开始数周的瑞典(sv-SE)文化。 已经使用en-US和sv-SE进行了测试。

    public static DateTime GetFirstDateOfWeek(int year, int weekOfYear, CultureInfo cultureInfo)
    {
        DateTime jan1 = new DateTime(year, 1, 1);
        int daysOffset = (int)cultureInfo.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
        DateTime firstWeekDay = jan1.AddDays(daysOffset);
        int firstWeek = cultureInfo.Calendar.GetWeekOfYear(jan1, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
        if (firstWeek == 1)
        {
            weekOfYear -= 1;
        }
        return firstWeekDay.AddDays(weekOfYear * 7);
    }