0

I have tried with the below solution, but i am not able to achieve it.

For example: 7, 2015 should return 15 Feb 2015 → 21 Feb 2015

private string GetWeekPeriod(string weekYear)
{
    CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
    int firstDayOfWek = (int)defaultCultureInfo.DateTimeFormat.FirstDayOfWeek;
    string result = string.Empty;
    string[] weekAndYear = weekYear.Split(new char[] { ',' });
    int week = Convert.ToInt32(weekAndYear[0]);
    int year = Convert.ToInt32(weekAndYear[1]);

    DateTime jan1 = new DateTime(year, 1, 1);
    int daysOffset = firstDayOfWek - (int)jan1.DayOfWeek;

    DateTime firstDayOfWeek = jan1.AddDays(daysOffset);
    var cal = CultureInfo.CurrentCulture.Calendar;
    var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
    int firstWeek = cal.GetWeekOfYear(firstDayOfWeek, dateTimeFormat.CalendarWeekRule, dateTimeFormat.FirstDayOfWeek);

    if (firstWeek <= 1)
    {
        week -= 1;
    }

    DateTime endDate = firstDayOfWeek.AddDays((week * 7) - 1);
    DateTime startDate = endDate.AddDays(-6);
    result = startDate.ToShortDateString() + " - " + endDate.ToShortDateString();

    return result;
}
Ocelot20
  • 10,510
  • 11
  • 55
  • 96
AMDI
  • 895
  • 2
  • 17
  • 40
  • 2
    I don't understand. How exactly 7th week of 2015 can be from 5 Feb to 11 Feb? – Soner Gönül May 27 '15 at 11:08
  • @SonerGönül I'm guessing that is on the basis that 2015 started on a Thursday, so all weeks start on a Thursday (rather than using locale rules) – Rowland Shaw May 27 '15 at 11:11
  • 1
    Additionally, you'll need to be *really* precise about week numbering - there are lots of different schemes. .NET doesn't quite support ISO-8601 for example. – Jon Skeet May 27 '15 at 11:11
  • 1
    @RowlandShaw Then the 5th of Februar wouldn't be the start of 7th week but of the 6th. – Ralf May 27 '15 at 11:15
  • ok,yes 2015starts on Thursday.Then how can i solve the above issue. – AMDI May 27 '15 at 11:16
  • @AJAYcHIGURUPATI What's not working about your attempt? – Rowland Shaw May 27 '15 at 11:18
  • sorry,I have edited my answer.For 7,2015 should return 15Feb-21Feb.With the above code,it is returning 8Feb-14Feb – AMDI May 27 '15 at 11:45
  • The question is too broad and also poorly defined. Please narrow down it to some practical cases and clarify the definition. In particular, are you looking for a universal solution to be compliant with ISO-8601 as @JonSkeet pointed out (re: http://en.wikipedia.org/wiki/ISO_week_date), etc. Best regards, – Alexander Bell May 27 '15 at 12:42
  • @AlexBell Yes Iam looking for Universal solution which will give me the correct week period once the weeknumber and year is given – AMDI May 27 '15 at 12:56
  • Take a look at this solution, it may help: http://stackoverflow.com/questions/19901666/get-date-of-first-and-last-day-of-week-knowing-week-number . Regards, – Alexander Bell May 27 '15 at 13:03
  • Thanks Alex, It worked.Thanks for the support – AMDI May 27 '15 at 13:34
  • Sure, you are welcome! Best of luck with your project. Regards, – Alexander Bell May 29 '15 at 16:28

1 Answers1

0

After followed the link give by @AlexBell.

I have to change the startDate and endDate to the following and it worked

DateTime startDate = firstDayOfWeek.AddDays(week * 7);
     DateTime endDate = startDate.AddDays(6);
AMDI
  • 895
  • 2
  • 17
  • 40