Load time on a RadDateInput

3 posts, 1 answers
  1. Steve LaForge
    Steve LaForge avatar
    61 posts
    Member since:
    Feb 2005

    Posted 27 Oct 2011 Link to this post

    I have a two RadDateInput fields: one to contain the date itself, and the other to contain the time in military format.  I also have a "Now" button that I want to allow the user to push and populate the fields with the current date and time.  The date field works, but the time does not.  The time field gets '00:00'.  I am trying to do this client-side to avoid the round-trip.

    The code in my ASPX page contains:

    function setReqDateNow() {
        var d = new Date();
        var reqDate = $find("<%= reqDate.ClientID %>");
        var reqTime = $find("<%= reqTime.ClientID %>");
        reqDate.set_selectedDate(d);
        reqTime.set_selectedDate(d);
    }
     
    ...
     
    <telerik:RadDateInput ID="reqDate" runat="server" DisplayDateFormat="MM/dd/yyyy" Width="75" BackColor="#fbf5bb" />
    <telerik:RadToolTip ID="tipReqDate" runat="server" HideEvent="LeaveTargetAndToolTip" TargetControlID="reqDate"
        Position="BottomRight" RelativeTo="Element" ShowEvent="OnMouseOver"
        Text="mm/dd/yyyy format less than or equal to today's date" />
       
    <telerik:RadDateInput ID="reqTime" runat="server" DisplayDateFormat="HH:mm" Width="50" BackColor="#fbf5bb" />
    <telerik:RadToolTip ID="tipReqTime" runat="server" HideEvent="LeaveTargetAndToolTip" TargetControlID="reqTime"
        Position="BottomRight" RelativeTo="Element" ShowEvent="OnMouseOver"
        Text="Enter time in hh:mm format" />
       
    <asp:Button ID="btnNow" runat="server" Text="Now" OnClientClick="setReqDateNow();return false;" />

    When the user clicks the button, it populates the date field, but it puts 00:00 in the time field.

    Thank you in advance for your help!
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2011 Link to this post

    Hello Steve,

    You can try the following javascript to set current time in DateInput.

    JS:
    <script type="text/javascript">
    function setReqDateNow()
    {
       var d = new Date();
       var reqDate = $find("<%= reqDate.ClientID %>");
       var reqTime = $find("<%= reqTime.ClientID %>");
       reqDate.set_selectedDate(d);
       var hr = d.getHours();
       var min = d.getMinutes();
       var tm = hr + ":" + min;
       reqTime.set_textBoxValue(tm);
    }
    </script>

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Steve LaForge
    Steve LaForge avatar
    61 posts
    Member since:
    Feb 2005

    Posted 28 Oct 2011 Link to this post

    Thank you Princy!  It wasn't the complete answer, but using set_textBoxValue method was what I was missing.  The only reason that it didn't completely solve the problem is that d.getHours() and d.getMinutes() returns only the significant digits.  For example, at 9:00 AM, it would set the string to '9:0', where what I really wanted was '09:00'.  To fix this issue, I found Javascript Right and Left functions, so I add a '0' (zero) to the front of each string, and then take the two right-most characters.  The working code looks like:

    function setReqDateNow() {
      var d = new Date();
      var reqDate = $find("<%= reqDate.ClientID %>");
      var reqTime = $find("<%= reqTime.ClientID %>");
      reqDate.set_selectedDate(d);
      var hr = Right("0" + d.getHours(), 2);
      var mn = Right("0" + d.getMinutes(), 2);
      reqTime.set_textBoxValue(hr + ":" + mn);
    }


    Thank you!
Back to Top