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

Combining date and time using a RadDatePicker and RadTimePicker; not a RadDateTimePicker

4 Answers 220 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Rome
Top achievements
Rank 1
Rome asked on 10 Apr 2009, 01:49 AM

Hello.  I’m trying to use a RadDatePicker and a RadTimePicker to pick a date and time separately and then combine the two into a variable (JavaScript).  The issue I’m having is that when I choose a date from the date popup of the RadDatePicker, it defaults to a time of 12:00am (00:00:00).  And when I choose a time from the time popup of the RadTimePicker, it defaults to the current date.  How can I combine the date and time that I choose without using a RadDateTimePicker?

 

Thank in advance.

4 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 13 Apr 2009, 01:23 PM
Hi Rome,

Combining two dates is a straightforward task if you use the standard Javascript methods:

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

Here is an example:

<%@ Page Language="C#" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>RadControls for ASP.NET AJAX</title> 
</head> 
<body> 
<form id="form1" runat="server"
<asp:ScriptManager ID="ScriptManager1" runat="server" /> 
 
<telerik:RadDatePicker ID="RadDatePicker1" runat="server" /> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 
<telerik:RadTimePicker ID="RadTimePicker1" runat="server" /> 
 
<br /><br /><br /> 
 
<asp:Button ID="Button1" runat="server" Text="Combine" OnClientClick="return Combine();" /> 
 
<asp:TextBox ID="TextBox1" runat="server" Width="300px" /> 
 
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"
<script type="text/javascript"
 
function Combine() 
    var dp = $find("<%= RadDatePicker1.ClientID %>").get_selectedDate(); 
    var tp = $find("<%= RadTimePicker1.ClientID %>").get_selectedDate(); 
     
    if (!dp || !tp) 
        return false; 
     
    var combinedDate = new Date(dp.getFullYear(), dp.getMonth(), dp.getDate(), tp.getHours(), tp.getMinutes(), tp.getSeconds()); 
     
    $get("<%= TextBox1.ClientID %>").value = combinedDate
     
    return false; 
 
</script> 
</telerik:RadCodeBlock> 
 
</form> 
</body> 
</html> 


All the best,
Dimo
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Rome
Top achievements
Rank 1
answered on 20 Apr 2009, 01:24 PM
Thank you Dimo.  This solution is what I needed. 

Rome
0
huejiitech
Top achievements
Rank 1
answered on 23 Sep 2013, 07:50 AM
How about in server side?
0
Shinu
Top achievements
Rank 1
answered on 23 Sep 2013, 08:55 AM
Hi Huejii,

Please try the following code snippet that I have tried to combine date and time in server side.

ASPX:
<telerik:RadDatePicker ID="RadDatePicker1" runat="server">
</telerik:RadDatePicker>
<br />
<telerik:RadTimePicker ID="RadTimePicker1" runat="server">
</telerik:RadTimePicker>
<br />
<telerik:RadButton ID="RadButton1" runat="server" OnClick="RadButton1_Click" Text="Combine">
</telerik:RadButton>
<br />
<telerik:RadTextBox ID="RadTextBox1" runat="server" Visible="false">
</telerik:RadTextBox>

C#:
protected void RadButton1_Click(object sender, EventArgs e)
{
    var date = RadDatePicker1.SelectedDate.Value;
    var time = RadTimePicker1.SelectedDate.Value;
    RadTextBox1.Visible = true;
    RadTextBox1.Text = date.Day + "-" + date.Month + "-" + date.Year + " " + time.Hour + ":" + time.Minute + ":" + time.Second;
}

Thanks,
Shinu.
Tags
Calendar
Asked by
Rome
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Rome
Top achievements
Rank 1
huejiitech
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Share this question
or