locked
How can I get the month of a week? RRS feed

  • Question

  • Hi,

    I know the year, week, and day of the week and I need the month and the date of day of the week.

     

    Thanks


    ADRIAN DIBU

    Saturday, September 28, 2013 2:39 AM

Answers

  • Hello adibu,

    Try to this code:

     static DateTime getdate(int year, int week, System.DayOfWeek dayofweek)
            {
                DateTime tDt = new DateTime(year, 1, 1);
                tDt.AddDays((week - 1) * 7);
    
                for (int i = 0; i <= 365; ++i)
                {
                    int tWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                        tDt,
                        CalendarWeekRule.FirstDay,
                        dayofweek);
                    if (tWeek == week)
                    {
                        Debug.WriteLine(tDt.Date);
                        Debug.WriteLine(tDt.Month);
                        Debug.WriteLine(tWeek);
                        return tDt;
                        break;
                    }
    
                    tDt = tDt.AddDays(1);
                }
                tDt = new DateTime();
                return tDt;
    
            }
    
    
     DateTime dt3 = getdate(2013,5,System.DayOfWeek.Saturday);
    This is simple method to get date using passing year, week, & your firstdayofweek. Hope this is helpful
    • Marked as answer by adibu Saturday, September 28, 2013 1:25 PM
    Saturday, September 28, 2013 5:30 AM

All replies

  • Add your year, week, and day to a Calendar object then call GetMonth and GetDayOfMonth.

    Saturday, September 28, 2013 3:17 AM
    Moderator
  • Hello adibu,

    Try to this code:

     static DateTime getdate(int year, int week, System.DayOfWeek dayofweek)
            {
                DateTime tDt = new DateTime(year, 1, 1);
                tDt.AddDays((week - 1) * 7);
    
                for (int i = 0; i <= 365; ++i)
                {
                    int tWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                        tDt,
                        CalendarWeekRule.FirstDay,
                        dayofweek);
                    if (tWeek == week)
                    {
                        Debug.WriteLine(tDt.Date);
                        Debug.WriteLine(tDt.Month);
                        Debug.WriteLine(tWeek);
                        return tDt;
                        break;
                    }
    
                    tDt = tDt.AddDays(1);
                }
                tDt = new DateTime();
                return tDt;
    
            }
    
    
     DateTime dt3 = getdate(2013,5,System.DayOfWeek.Saturday);
    This is simple method to get date using passing year, week, & your firstdayofweek. Hope this is helpful
    • Marked as answer by adibu Saturday, September 28, 2013 1:25 PM
    Saturday, September 28, 2013 5:30 AM
  • Hi,

    Thanks Khant Nipur for thr prompt and explicit answer.

    It what I wanted

    Best regards


    ADRIAN DIBU

    Saturday, September 28, 2013 1:25 PM