Dxtr Dxtr - 1 year ago 142
Javascript Question

get the value of bootstrap-Datetimepicker in javascript

I need to get the value of Datetimepicker in my java-script function , i made something like this but it didn't work :

$("#date").click( function(){

it gives me 'undefined'
Can you please help me with this

Ian Ian
Answer Source

Either use:


Or (from looking at the page source):


The returned value will be a Date (for the first example above), so you need to format it yourself:

var date = $("#datetimepicker1").data("datetimepicker").getDate(),
    formatted = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours + ":" + date.getMinutes() + ":" + date.getSeconds();

Also, you could just set the format as an attribute:

<div id="datetimepicker1" class="date">
    <input data-format="yyyy-MM-dd hh:mm:ss" type="text"></input>

and you could use the $("#datetimepicker1").find("input").val();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download