I am trying to utilize tempusdominus-datetimepicker-3 to create a date-time picker in my html-forms.
However, I need to be able to get the selected date from it. The plugin has an option called date which according to the document should return a moment object or null. Here is what the document say about this option
Returns the component's model current date, a
moment
object ornull
if not set
However, I am struggling to access the date
option.
Also from the doc
Note All options are accessed via the
data
attribute e.g.$('#datetimepicker').datetimepicker(OPTION, ARGUMENT)
So I tried the following to access the date option.
from = $('#datetimepicker').datetimepicker('date');
from = $('#datetimepicker').datetimepicker('data', 'date');
from = $('#datetimepicker').datetimepicker('data').date;
from = $('#datetimepicker').datetimepicker(function(e){
return e.date;
});
But none of the above is returning the object. How can I access the date object?
I would think a nice plugin like this one will have more readable option like getDate()
, setDate(date)
, getFomat()
and setFormat(...)
etc; or event examples, which should eliminate questions like this one, but unfortunately it does not.
Bootstrap Date Picker Bootstrap date picker is a plugin that adds the function of selecting time without the necessity of using custom JavaScript code. This documentation may contain syntax introduced in the MDB 4.17.0 and can be incompatible with previous versions. For old Date Picker documentation please follow the link.
The datetimepicker Bootstrap plug-in also includes a CSS file that specifies the default look and feel of the calendar / time picker. By picking the specific classes and modifying the default properties as per need, you may override the default classes.
A datetimepicker will be added to the “#form_datetime” input field. Also, all earlier dates will be disabled for preventing user select a previous date. Once the form is submitted, the event name and event date & time will be inserted into the database with PHP and MySQL.
Once the user selects a date & time, the datetimepicker will be closed automatically. The Today link will appear under the datetimepicker to select today’s date and the current time. Bootstrap DateTime Picker has various options for datetimepicker method, you can get the details from here.
Here is the correct code to access events :
$(function() {
$('#datetimepicker').datetimepicker();
$('#datetimepicker').on("change.datetimepicker", function (e) {
console.log(e.date);
});
});
You can get date using this too :
var date = $('#datetimepicker').datetimepicker('viewDate')
Complete fiddle to manipulate the date : https://jsfiddle.net/10xzksm0/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With