How to get the week number of date

Recently I faced a problem in my project that I have 52 images and I have to display images based on the week of year. So as a solution I created images with image name like image1.jpg,image2.jpg and so on. And created a small function which returns the week of year. Here is the code through which I calculated the week of year after searching it on net for a while:-

/// <summary>

/// Gets the week number.

/// </summary>

/// <param name="dtDate">The date for which week number is required.</param>

/// <returns></returns>

public static int GetWeekNumber(DateTime dtDate)

{

    CultureInfo culture = CultureInfo.CurrentCulture;

    int weekNumber= culture.Calendar.GetWeekOfYear(dtDate, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);

    return weekNumber;

}


After this function I placed a server side Image control on my aspx page like this:-
<asp:Image ID="imgForWeek" runat="server" />

And here is how I called my function to display image for week in my code behind file:-

protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

        imgForWeek.ImageUrl = "../Images/image" + GetWeekNumber(DateTime.Now)+".jpg";

    }

}


Hopefully this will be useful for you as well. Happy Coding !!!