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

RadCalendar WeekNumber

4 Answers 267 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Rob Venable
Top achievements
Rank 1
Rob Venable asked on 22 Apr 2010, 03:02 AM
Hi,
Is it possible to return the WeekNumber of a RadCalendar on PostBack?
I need to pass the weeknumber (and probably the year also) to my function so that I can return sales for this week.

Is this possible?

Thanks

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Apr 2010, 08:03 AM
Hi Rob,

Here is the code snippet to retrieve the WeekNumber of selected date of Calendar.

CS:
 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
        DateTime dt = (DateTime)RadCalendar1.SelectedDate;     
        int number = GetWeekNumber(dt);  // Week number 
        int year = dt.Year;   // Year 
    } 
    public int GetWeekNumber(DateTime dtDate) 
    { 
        CultureInfo ciCurr = CultureInfo.CurrentCulture; 
        int weekNum = ciCurr.Calendar.GetWeekOfYear(dtDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); 
        return weekNum+1; 
    } 


Regards,
Princy.
0
Rob Venable
Top achievements
Rank 1
answered on 23 Apr 2010, 12:36 AM
Thanks Princy. That's exactly what I needed.
0
@CC
Top achievements
Rank 1
answered on 08 Nov 2010, 07:09 PM
Hello:

RadDatePicker with WeekNo showing:
<telerik:RadDatePicker ID="uxDatePicker" runat="server" AutoPostBack="true" OnSelectedDateChanged=OnSelectedDateChanged Skin="Windows7" Culture="English (United States)" >
   <Calendar ID="Calendar1" runat="server" ShowColumnHeaders="true" ShowRowHeaders="true" >
      <ClientEvents OnLoad="AddEvent" />
   </Calendar>
</telerik:RadDatePicker>

Based on the above code by princy, the week no does not match with the radDatePicker's week number for say '12/28/2010' the function returns 53 whereas radDatePicker says 1. Any suggestions?

0
Vasil
Telerik team
answered on 10 Nov 2010, 04:23 PM
Hi,

Actually 12/28/2010 is in 52 week, in both US and ISO standards. So both 51 and 1 are wrong.

Try the following code-snippets:

Aspx:
<telerik:RadDatePicker ID="RadDatePicker1" AutoPostBack="true" runat="server" OnSelectedDateChanged="RadDatePicker1_SelectedDateChanged">
    <Calendar runat="server">
    </Calendar>
    <DateInput runat="server" DisplayDateFormat="M/d/yyyy" DateFormat="M/d/yyyy" AutoPostBack="True">
    </DateInput>
    <DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton>
</telerik:RadDatePicker>
<label id="Label1" runat="server">
</label>

Cs:
protected void Page_Load(object sender, EventArgs e)
{
    CultureInfo c = new CultureInfo("en-US");
    c.DateTimeFormat.CalendarWeekRule = CalendarWeekRule.FirstFourDayWeek;
    c.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;
    RadDatePicker1.Calendar.CultureInfo = c;
}
 
protected void RadDatePicker1_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
{
    DateTime dt = (DateTime)RadDatePicker1.SelectedDate;
    CultureInfo c = RadDatePicker1.Calendar.CultureInfo;
    int number = c.Calendar.GetWeekOfYear(dt, c.DateTimeFormat.CalendarWeekRule, c.DateTimeFormat.FirstDayOfWeek);
    Label1.InnerText = number.ToString();
}

This should get you the right day when you use GetWeekOfYear.

Be aware that you still will have divergence in some cases.
For example:
    12/31/2012 is 53 week by the ISO standard.
    01/01/2013 is 1 week by the ISO standard.
Both this days, however are in the same week. In calendar week column, you can see only one number. It can not show 53 and 1 at the same time. And it shows 1.

All the best,
Vasil
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Calendar
Asked by
Rob Venable
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rob Venable
Top achievements
Rank 1
@CC
Top achievements
Rank 1
Vasil
Telerik team
Share this question
or