This is a migrated thread and some comments may be shown as answers.

Getting the week number for a selected date

4 Answers 279 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 1
Allan asked on 19 Mar 2009, 02:27 PM
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

4 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 19 Mar 2009, 06:50 PM
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.
0
Kent Hoang
Top achievements
Rank 1
answered on 07 Dec 2009, 08:38 PM
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();

}

0
Veselin Vasilev
Telerik team
answered on 11 Dec 2009, 10:23 AM
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.
0
Sebastian
Telerik team
answered on 11 Dec 2009, 11:27 AM
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.
Tags
DatePicker
Asked by
Allan
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
Kent Hoang
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Sebastian
Telerik team
Share this question
or