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

challenge to set the DateTimePicker selected date on client side

4 Answers 52 Views
DateTimePicker
This is a migrated thread and some comments may be shown as answers.
Miguel
Top achievements
Rank 1
Miguel asked on 17 Dec 2016, 12:15 AM

Hi,

My application receives parameters from a query string, so these are all strings.

The problem that I have is that in my form, there is a DateTimePicker which needs to be updated with one of these values.

The client side API, set_date, expects a date, and honestly I do not know how to convert a string (or a single) to a date from client side java script. Is this possible?

4 Answers, 1 is accepted

Sort by
0
Patrick | Technical Support Engineer, Senior
Telerik team
answered on 19 Dec 2016, 10:15 PM
Hi Miguel,

You can convert a string to a date in JavaScript using Date.parse().  To set the value of the RadDateTimePicker, use set_selectedDate().
function pageLoad(sender, eventArgs) {
 
    var myStringDate = "Dec 15 2016 5:36 PM";
    var myParsedDate = new Date(Date.parse(myStringDate));
    var datetimepicker = $find("<%=RadDateTimePicker1.ClientID %>");
 
    datetimepicker.set_selectedDate(myParsedDate);
}

I've attached a project which demonstrates the approach above.

I hope this helps!

Regards,
Patrick
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Miguel
Top achievements
Rank 1
answered on 20 Dec 2016, 10:25 PM

Hi Patrick it works beautiful, thank you very much.

Can you please explain why this two sentences does not work the same? it is an error that I had in my code

var dtp = document.getElementById('<%= dtpReceived.ClientID %>'); (does not work to use set_selectedDate)

var dtp = $find("<%=dtpReceived.ClientID %>"); (works OK to use set_selectedDate)

0
Accepted
Patrick | Technical Support Engineer, Senior
Telerik team
answered on 21 Dec 2016, 09:14 PM
Hi Miguel,

In order to use the getElementById() method, you should also include the element's control property.  
var datetimepicker = document.getElementById("<%=RadDateTimePicker1.ClientID %>").control;

This is because the client-side reference is assigned to the wrapper DOM element and exposed through the control property.  

I hope this clears things!

Regards,
Patrick
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Miguel
Top achievements
Rank 1
answered on 09 Jan 2017, 04:00 PM
thanks a lot
Tags
DateTimePicker
Asked by
Miguel
Top achievements
Rank 1
Answers by
Patrick | Technical Support Engineer, Senior
Telerik team
Miguel
Top achievements
Rank 1
Share this question
or