javascriptasp.nettelerikraddatepicker

Deduct Days from RadDateTimePicker


Does anyone know how to deduct 7 days from the current date in RadDateTimePicker using Javascript?

I didn't find any example on how you deduct days from a RadDateTimePicker.

I would love if anyone can show me examples.

Aspx.code

<table border="0">
    <tr>
        <td>From:</td>
        <td>
            <telerik:RadDateTimePicker ID="rdpDateFrom" runat="server" Enabled="True" Width="170px">
                <Calendar ID="fromCalendar" runat="server" EnableKeyboardNavigation="True">
                    <SpecialDays> 
                        <telerik:RadCalendarDay Repeatable="Today" ItemStyle-BackColor="lightblue"></telerik:RadCalendarDay> 
                    </SpecialDays> 
                </Calendar>
                <DateInput ID="DateInput3" ToolTip="From Date input" runat="server"/>                                        
            </telerik:RadDateTimePicker>
        </td>
    </tr>
    <tr>
        <td>To:</td>
        <td>
            <telerik:RadDateTimePicker ID="rdpDateTo" runat="server" Enabled="True" Width="170px">
                <Calendar ID="toCalendar" runat="server" EnableKeyboardNavigation="True" />                                                                                        
                <DateInput ID="DateInput4" ToolTip="To Date input" runat="server"/>
            </telerik:RadDateTimePicker>     
        </td>
    </tr>
</table>

JavaScript Code

function OnClientSelectedIndexChanged(sender, eventArgs) {
    var range = $find("<%= rcbDateTime.ClientID %>");
    var item = eventArgs.get_item();
    var From = $find("<%= rdpDateFrom.ClientID %>");
    var To = $find("<%= rdpDateTo.ClientID %>");
    var todaysDate = new Date();
    todaysDate.setDate(todaysDate.getDate());
    if (item.get_value() == "1") { //Today's Date
        From.set_selectedDate(todaysDate);
        To.set_selectedDate(todaysDate);
    }
    if (item.get_value() == "2") { //Last 7 days
        //Calculation 

        From.set_selectedDate(
        To.set_selectedDate(todaysDate);
    }
}

Solution

  • More of a comment than an answer:

    If I understand the issue (and maybe I don't, there's documentation here), you need to pass date objects to the set_selectedDate method. There seems to be some typos in the OP, so maybe best to just post the corrected code, something like:

        var itemValue = item.get_value();
        var todaysDate = new Date();
        var weekAgo = new Date(+todaysDate);
        weekAgo.setDate(weekAgo.getDate() - 7);
    
        //Today's Date
        if (itemValue == "1") {
            From.set_selectedDate(todaysDate);
            To.set_selectedDate(todaysDate);
    
        //Last 7 days
        } else if (itemValue == "2") {
            From.set_selectedDate(todaysDate);
            To.set_selectedDate(weekAgo);
        }
    

    If there are more conditions, you might consider using switch instead of if..else.