Calculate difference in weeks using RadDatePicker Control in Javascript

3 posts, 0 answers
  1. Rahul Khinvasara
    Rahul Khinvasara avatar
    303 posts
    Member since:
    Aug 2007

    Posted 26 Aug 2009 Link to this post

    Hi,
        I am using a RadDatePicker Control on my page. I have to calculate the difference in weeks between the date selected in this RadDatePicker Control and Current Date (i.e Today's Date). I have to display this difference (in weeks)  inside a textbox on selecting a date from the RadDatePicker Control. How can I do this ?


    I have used the following html code for RadDatePicker Control in my application :- 

    <telerik:RadDatePicker ID="dtDate" runat="server" DateInput-DateFormat="dd/MM/yyyy" Width="120px" Skin="WebBlue">   
    <DateInput DateFormat="dd/MM/yyyy" LabelCssClass="radLabelCss_WebBlue" Skin="WebBlue" runat="server" ID="DateInput6" >   
    </DateInput>   
    <Calendar Skin="WebBlue" Width="50px" Height="50px">   
    </Calendar>   
    <DatePopupButton CssClass="radPopupImage_WebBlue" />   
    </telerik:RadDatePicker> 
     
     
     
     
     
     
     
     
     

     

     

     

     

     

    Thanks

     

     

     

     

  2. Dimo
    Admin
    Dimo avatar
    8333 posts

    Posted 26 Aug 2009 Link to this post

    Hello Rahul,

    Your task is to calculate the difference between two Javascript Date objects. In order to do that, please get familiar with the Javascript Date object API:

    http://www.w3schools.com/jsref/jsref_obj_date.asp


    After you create your calculation algorithm, use the RadDatePicker's DateSelected client event to get the newly selected date:

    http://www.telerik.com/help/aspnet-ajax/calendar_clientsideondateselected.html

    <telerik:RadDatePicker ID="RadDatePicker1" runat="server">
        <ClientEvents OnDateSelected="MyDateSelected" />
    </telerik:RadDatePicker>


    Regards,
    Dimo
    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. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Aug 2009 Link to this post

    Hi Rahul,

    Try the following client side code for retrieving the difference in weeks numbers between today's and selected date.

    ASPX:
     
    <telerik:RadCalendar ID="RadCalendar1" runat="server"
    <ClientEvents OnDateSelected="OnDateSelected" /> 
    </telerik:RadCalendar> 

    JavaScript:
     
    <script type="text/javascript"
    function Week() 
        Date.prototype.getMDay = function() 
        { 
        return (this.getDay() + 6) %7;} 
        Date.prototype.getISOYear = function() { 
        var thu = new Date(this.getFullYear(), this.getMonth(), this.getDate()+3-this.getMDay()); 
        return thu.getFullYear();} 
        Date.prototype.getISOWeek = function() { 
        var onejan = new Date(this.getISOYear(),0,1); 
        var wk = Math.ceil((((this - onejan) / 86400000) + onejan.getMDay()+1)/7); 
        if (onejan.getMDay() > 3) wk--;return wk;}  
        var today = new Date(); 
        return today.getISOWeek(); 
    function OnDateSelected(sender, eventArgs) 
    {   
        var WeekOfYear = eventArgs.get_renderDay().RadCalendarView.GetWeekOfYear(eventArgs.get_renderDay().get_date()) 
        var WeekOfToday = Week(); 
       // alert(WeekOfYear); 
       // alert(WeekOfToday); 
        var diff = WeekOfToday - WeekOfYear; 
        alert(diff); 
    </script> 

    -Shinu.
Back to Top