1

I have a datetimepicker and a button. The user selects a date time, then hit the button. The selected date time is shown in an alert.

https://jsfiddle.net/jeffxiao/rfzm8pyt/

$(function() {
  $('#datetimepicker1').datetimepicker();

  $('#buttonGo').click(function() {
      var moment = $("#datetimepicker1").datetimepicker("date");
      alert(moment.format('MMMM Do YYYY, h:mm:ss a'));
  })
});

The question is, if the user changes the text for the input, instead of choosing from datetimepicker, is there a way to retrieve the updated datetime (in the text)?

My test shows the default is to retrieve the selected in the picker, NOT the updated text in the text box.

XoXo
  • 1,560
  • 1
  • 16
  • 35

2 Answers2

2

I had a similar issue and I found that using viewDate instead of date solved the issue. So your sample code would be updated to

$(function() {
  $('#datetimepicker1').datetimepicker();

  $('#buttonGo').click(function() {
      var moment = $("#datetimepicker1").datetimepicker("viewDate");
      alert(moment.format('MMMM Do YYYY, h:mm:ss a'));
  })
});

Edit: On further investigation I found that the above seems to only work if you don't have useCurrent: false set. If useCurrent: false is set then viewDate appears to return the time when the datetimepicker was initialized.

Kyle
  • 69
  • 1
  • 6
0

Does that help ? Set an id to your input to retrieve data.

https://jsfiddle.net/djibe89/kx1rnbm9/

$(function() {
  $('#datetimepicker1').datetimepicker();

  $('#buttonGo').click(function() {
      var time = $("#input-datetimepicker").val();
      alert(time);
  })
});
djibe
  • 2,753
  • 2
  • 17
  • 26