challenge to set the DateTimePicker selected date on client side

5 posts, 1 answers
  1. Miguel
    Miguel avatar
    15 posts
    Member since:
    Mar 2011

    Posted 16 Dec 2016 Link to this post

    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?

  2. Patrick
    Admin
    Patrick avatar
    193 posts

    Posted 19 Dec 2016 Link to this post

    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.
  3. Miguel
    Miguel avatar
    15 posts
    Member since:
    Mar 2011

    Posted 20 Dec 2016 in reply to Patrick Link to this post

    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)

  4. Answer
    Patrick
    Admin
    Patrick avatar
    193 posts

    Posted 21 Dec 2016 Link to this post

    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.
  5. Miguel
    Miguel avatar
    15 posts
    Member since:
    Mar 2011

    Posted 09 Jan in reply to Patrick Link to this post

    thanks a lot
Back to Top