Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of bootstrap Datetimepicker in JavaScript

I need to get the value of Datetimepicker in my JavaScript function. I have made something like this, but it doesn't work:

$("#date").click( function(){     alert(document.getElementById('datetimepicker1').value); }); 

It gives me 'undefined'

like image 500
dxtr Avatar asked May 15 '13 19:05

dxtr


People also ask

How do I get the datepicker value?

If you want to get the date when the user selects it, you can do this: $("#datepicker"). datepicker({ onSelect: function() { var dateObject = $(this). datepicker('getDate'); } });

What does DateTimePicker return?

It returns a DateTime value DateTime date1 = datetimepicker1.Value; Follow this answer to receive notifications.

How do I use Bootstrap time Picker?

If we want to add DateTimePicker in our input field and our application uses the Bootstrap framework, we can easily add this by using the jQuery plugin of Bootstrap DateTimePicker. With bootstrap DateTimePicker plugin's help, we can easily add DateTimePicker in the form's input element by jQuery.


1 Answers

Either use:

$("#datetimepicker1").data("datetimepicker").getDate(); 

Or (from looking at the page source):

$("#datetimepicker1").find("input").val(); 

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(); alert(formatted); 

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> </div> 

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

like image 112
Ian Avatar answered Sep 19 '22 16:09

Ian