2014-08-28 55 views
0

有誰知道如何從使用JavaScript的RadDateTimePicker中的當前日期扣除7天?從RadDateTimePicker扣除天

我沒有找到任何關於如何從RadDateTimePicker中扣除天數的例子。

如果有人能給我看例子,我會很樂意。

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代碼

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); 
    } 
} 
+0

'todaysDate.setDate(todaysDate.getDate());'就像說'x = x',它不會改變* todaysDate *的值。如果你想在7天前設置一個日期,那麼'var weekAgo = new Date(+ todaysDate); weekAgo.setDate(weekAgo.getDate() - 7);'。 – RobG 2014-08-28 23:01:42

+0

我試了一下我收到了長號碼功能 var todaysDate = new Date(); if(item.get_value()==「2」){// Last 7 days // Calculation var weekAgo = new Date(+ todaysDate); var result = weekAgo.setDate(weekAgo.getDate() - 7); From.setDate(result); To.set_selectedDate(todaysDate); } } – 2014-08-28 23:21:59

回答

1

以上回答的評論:

如果我沒有理解這個問題(也許我不這樣做,有文檔here),您需要將日期對象傳遞給set_selectedDate方法。似乎是在OP一些錯別字,所以也許最好只發布更正後的代碼,類似:

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..else