-2

I am using the following method to retrieve the week.

public int GetWeek(DateTime date)
{
    var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
    return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);            
}

If the week number is between 0 to 9, it should return 00 - 09 as the week number. How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1120418
  • 261
  • 3
  • 8
  • 18
  • 4
    `00` and `09` sound more like string sthan numbers to me... – doctorlove Jul 28 '14 at 12:05
  • This question is unrelated to weeks (in particular, it is not about *retrieving* the week number), or any thing related to dates, for that matter. It is a duplicate of the linked question about number formatting. That said, a *formatted* number is a `string`, not an `int` any more. – O. R. Mapper Jul 28 '14 at 12:06
  • @SujithKarivelil: Please read the question. The OP is *not* asking about how to find out or compute a week number. – O. R. Mapper Jul 28 '14 at 12:12
  • @ O. R. Mapper : see my answer – Suji Jul 28 '14 at 12:29
  • @SujithKarivelil: There does not seem to be any answer written by you on the question here. – O. R. Mapper Jul 28 '14 at 12:52

1 Answers1

1

I presume that, as you are looking for a leading zero, you are looking to format these numbers.

If so, simply use int.ToString() to format your GetWeek method:

 public int GetWeek(DateTime date)
    {
        var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
        return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);            
    }

public string GetWeekFormatted(DateTime date)
{
    return GetWeek(date).ToString("00");
}
KaraokeStu
  • 758
  • 7
  • 17