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

Load time on a RadDateInput

2 Answers 59 Views
Input
This is a migrated thread and some comments may be shown as answers.
Steve LaForge
Top achievements
Rank 1
Steve LaForge asked on 27 Oct 2011, 11:25 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 28 Oct 2011, 06:43 AM
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.
0
Steve LaForge
Top achievements
Rank 1
answered on 28 Oct 2011, 08:42 PM
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!
Tags
Input
Asked by
Steve LaForge
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Steve LaForge
Top achievements
Rank 1
Share this question
or