7

How can I get the start and end date of a given year (int), given month (int), and given week (int) { example Year : 2011 Month: 07 week: 04 } in c# 4.0? Thanks in advance.

The Start Date of the Year 2011 Month 07 and the week number of the month is 04.

Nishan
  • 93
  • 1
  • 1
  • 5
  • 2
    What are your rules for when a "week" starts? There are *lots* of possibilities there. – Jon Skeet Jul 29 '11 at 11:01
  • Wait wait. Is your question "How can I get the start and end date of a week, given the year, month and week number"??? I don't get what the start and end date of a given year means. Isn't it just Jan 1 and Dec 31? If that IS your question, see this: http://stackoverflow.com/questions/662379/calculate-date-from-week-number – Yuf Jul 29 '11 at 20:25

5 Answers5

10

Google is your friend.

Months:

public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
{
   return new DateTime(dateTime.Year, dateTime.Month, 1);
}


public DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
{
   DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
   return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}

You can do something similar for years:

   DateTime time = new DateTime(2011,1,1);
   time.AddYears(1).AddDays(-1);

And week needs to use the CultureInfo.FirstDay (or whatever you want to set as the first day of a week, in some countries it's Monday, sometimes it's Sunday).

/// <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;
    }
Carra
  • 17,808
  • 7
  • 62
  • 75
  • Useful code fragment, but I don't think it relate to the original question at all (except you can use FirstDayOfMonthFromDateTime(), I suppose). – Steve Morgan Jul 29 '11 at 11:10
0

Not sure, but is this what you're after?

var weekStart = new DateTime(year, month, 1).AddDays(week * 7);
var weekEnd = weekStart.AddDays(6);
George Duckett
  • 31,770
  • 9
  • 95
  • 162
0

Assuming you start with week 1:

var startDate = new DateTime(year, month, 1).AddDays((week - 1) * 7);
var endDate = startDate.AddDays(6);
Steve Morgan
  • 12,978
  • 2
  • 40
  • 49
0

You could also use

DateTime.DaysInMonth(int year,int month);

to figure it out. The weeks will be more difficult.

Lysgaard
  • 234
  • 3
  • 11
0

DateTime calculations as these are a bit tricky, with some assumptions i could come up with this

//assign it to the first day of the month
DateTime getweek = new DateTime(2011, 4, 1);
// say the week starts on a Sunday
while (getweek.DayOfWeek != DayOfWeek.Sunday)
      getweek = getweek.AddDays(1);
DateTimeFormatInfo info = DateTimeFormatInfo.CurrentInfo;            
Calendar cal = info.Calendar;
//Now you are on the first week add 3 more to move to the Fourth week
DateTime start = cal.AddWeeks(getweek, 3); // 24 April 2011
DateTime end = start.AddDays(6); // 30 April 2011
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82