Getting the week number for a selected date

5 posts, 0 answers
  1. Allan
    Allan avatar
    8 posts
    Member since:
    Jan 2008

    Posted 19 Mar 2009 Link to this post

    I would like to know if it is possible to get the value of the week column for the sleected date? The datepicker shows the week number and rather than having to calculate it myself i would like to "reuse" the value.

    Also, is there any way of inputting the week number and get the first date of that week?

    Regards
    Allan Michaelsen

    I'm using the Silverlight 2009 Q1 package

  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 19 Mar 2009 Link to this post

    Hello Allan,

    There is no easy way to reuse the WeekNumber value outside of the RadCalendar, but here is how the calendar calculates the week number, hopefully you will be able to reuse it where necessary:

    //RadCalendar is an instance of the calendar:  
     
    //Get the specific calendar culture or use the current one:  
    CultureInfo cultureToUse = myCalendar.Culture ?? CultureInfo.CurrentCulture;  
     
    //Get the custom CalendarWeekRule or use the culture-specific:  
    CalendarWeekRule calendarWeekRuleToUse = myCalendar.CalendarWeekRule ?? cultureToUse.DateTimeFormat.CalendarWeekRule;  
     
    //Do the same for the FirstDayOfWeek:  
    DayOfWeek firstDayOfWeekToUse = myCalendar.FirstDayOfWeek ?? cultureToUse.DateTimeFormat.FirstDayOfWeek;  
     
    var selectedDate = myCalendar.SelectedDate.Value;  
     
    //Get the week number in a culture-specific safe way:  
    var weekNum = cultureToUse.Calendar.GetWeekOfYear(selectedDate, calendarWeekRuleToUse, firstDayOfWeekToUse);  
     

    Also here is how you can get the date of a first day of a week:

    //Get the first date of a week is culture specific way uses some enum calculations:  
    var firstDateOfWeek = selectedDate;  
    if (firstDayOfWeekToUse < selectedDate.DayOfWeek)  
    {  
        firstDateOfWeek = selectedDate.AddDays((int)firstDayOfWeekToUse - (int)selectedDate.DayOfWeek);  
    }  
    else if (firstDayOfWeekToUse > selectedDate.DayOfWeek)  
    {  
        firstDateOfWeek = selectedDate.AddDays(-(7-(int)firstDayOfWeekToUse + (int)selectedDate.DayOfWeek));  
    }  
     


    It may be a good idea to include these as helper methods for the Calendar, we will consider that.

    All the best,
    Miroslav
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Kent Hoang
    Kent Hoang avatar
    7 posts
    Member since:
    Sep 2009

    Posted 07 Dec 2009 Link to this post

    hi,

    I was using this code below to get the weeknumber but it's not working.  Can you help?

    protected

     

    void RadDatePicker1_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)

     

    {

     

    CultureInfo cultureToUse = RadDatePicker1.Calendar.CultureInfo;

     

     

    CalendarWeekRule calendarWeekRuleToUse = RadDatePicker1.Calendar.DateTimeFormat.CalendarWeekRule;

     

     

    DayOfWeek firstDayOfWeekToUse = RadDatePicker1.Calendar.DateTimeFormat.FirstDayOfWeek;

     

     


    string
    weekNum = cultureToUse.Calendar.GetWeekOfYear(RadDatePicker1.Calendar.SelectedDate, calendarWeekRuleToUse, firstDayOfWeekToUse).ToString();

     

    Label18.Text = weekNum.ToString();

    }

  5. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 11 Dec 2009 Link to this post

    Hi Kent Hoang,

    This is a forum about RadDatePicker for Silverlight. It seems your question is related to the ASP.NET Ajax control. Could you please re-post your question in the appropriate forum to avoid misleading the other community members of this forum.

    Thank you

    Sincerely yours,
    Veselin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 11 Dec 2009 Link to this post

    Hello Kent,

    This is quick follow-up.

    Your approach seems to be correct and conforms to the guidelines provided in this forum post. We will appreciate if you start a new thread under the RadCalendar for ASP.NET AJAX forum and provide details about what fails during the code execution or what the incorrect result you get is. Thus we will do our best to advice you further.

    Best regards,
    Sebastian
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
DevCraft banner