Royi Namir
Royi Namir

Reputation: 148714

Get the dates for GetWeekOfYear in c#?

I have this function which gives me ( for a current date ) - its week num :

so for : DateTime(2009,1,1)

 CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(new DateTime(2009,1,1), CalendarWeekRule.FirstDay, DayOfWeek.Sunday).Dump();

answer : 1

and for : DateTime(2009,1,4)

answer : 2

enter image description here

now , I need a function which gives me the startDate && endDate for this values :

so for week #1 -> 1/1/2009   ---> 1/3/2009
so for week #2 -> 1/4/2009   ---> 1/10/2009

Hence : i have a function which gives me the week num for a specified date. but this week spans from x---> y

I need those x and y.

thanks.

p.s. - i've been searching for a func like this , and didn't find . :-(

Upvotes: 3

Views: 2874

Answers (3)

user743382
user743382

Reputation:

To get the first day in the week, by date:

static DateTime GetFirstDayOfWeek(DateTime date)
{
    var firstDayOfWeek = date.AddDays(-((date.DayOfWeek - DayOfWeek.Sunday + 7) % 7));
    if (firstDayOfWeek.Year != date.Year)
        firstDayOfWeek = new DateTime(date.Year, 1, 1);
    return firstDayOfWeek;
}

The last day of the week works the same way:

static DateTime GetLastDayOfWeek(DateTime date)
{
    var lastDayOfWeek = date.AddDays((DayOfWeek.Saturday - date.DayOfWeek + 7) % 7);
    if (lastDayOfWeek.Year != date.Year)
        lastDayOfWeek = new DateTime(date.Year, 12, 31);
    return lastDayOfWeek;
}

Royi's addition ( final) :

extension method which gives you all the details ( week details) from a single date : p.s. first day of week = sunday.

   public class DateTimeSpan
    {
        public DateTime WeekStartDate;
        public DateTime WeekEndDate;
        public DateTime MonthStartDate;
        public DateTime MonthEndDate;
        public DateTime YearStartDate;
        public DateTime YearEndDate;
        public int WeekNum;

    }

    public static DateTimeSpan TimeProperties(this DateTime str)
    {
        if (str == null) return null;
        DateTimeSpan dts = new DateTimeSpan();
        dts.WeekNum=     CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(str, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        dts.WeekStartDate = GetFirstDayOfWeek(str);
        dts.WeekEndDate = GetLAstDayOfWeek(str);
        dts.MonthStartDate = new DateTime(str.Year, str.Month, 1);
        int numberOfDays = DateTime.DaysInMonth(str.Year, str.Month);
        DateTime last = new DateTime(str.Year, str.Month, numberOfDays);
        dts.MonthEndDate = last;
        dts.YearStartDate = new DateTime(str.Year, 1, 1);
        numberOfDays = DateTime.DaysInMonth(str.Year, 12);
        last = new DateTime(str.Year, 12, numberOfDays);
        dts.YearEndDate = last;

        return dts;
    }


    static DateTime GetFirstDayOfWeek(DateTime date)
    {
        var firstDayOfWeek = date.AddDays(-((date.DayOfWeek - DayOfWeek.Sunday + 7) % 7));
        if (firstDayOfWeek.Year != date.Year)
            firstDayOfWeek = new DateTime(date.Year, 1, 1);
        return firstDayOfWeek;
    }

    static DateTime GetLAstDayOfWeek(DateTime date)
    {
        var firstDayOfWeek = date.AddDays(((DayOfWeek.Saturday - date.DayOfWeek + 7) % 7));
        if (firstDayOfWeek.Year != date.Year)
            firstDayOfWeek = new DateTime(date.Year, 12, 31);
        return firstDayOfWeek;
    }

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48590

using System;
using System.Globalization;

public static class FirstDayOfWeekUtility
{
    /// <summary>
    /// Returns the first day of the week that the specified
    /// date is in using the current culture. 
    /// </summary>
    public static DateTime GetFirstDayOfWeek(DateTime dayInWeek)
    {
        CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
        return GetFirstDateOfWeek(dayInWeek, defaultCultureInfo);
    }

    /// <summary>
    /// Returns the first day of the week that the specified date 
    /// is in. 
    /// </summary>
    public static DateTime GetFirstDayOfWeek(DateTime dayInWeek, CultureInfo cultureInfo)
    {
        DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
        DateTime firstDayInWeek = dayInWeek.Date;
        while (firstDayInWeek.DayOfWeek != firstDay)
            firstDayInWeek = firstDayInWeek.AddDays(-1);

            return firstDayInWeek;
    }
}

Upvotes: 1

Frederik Gheysels
Frederik Gheysels

Reputation: 56964

I once used one of the method described in the comments of this post http://joelabrahamsson.com/entry/getting-the-first-date-in-a-week-with-c-sharp

Upvotes: 1

Related Questions