Setting date and time in jQuery datetimepicker

I have a situation where I try to use the Trent Richardson jQuery datetimepicker extension so that users can select dates and times at a time.

But before that, I want to set the date and time so that the default date and time that will be set from the server is displayed in the datetimepicker input field. Then the user will click inside the input field to call the datetimepicker, after which the default date and time will appear on the datetimepicker. The user can then override them if necessary.

So far in my code I have:

<script type="text/javascript"> $(function(){ $('.example-container > pre').each(function(i){ eval($(this).text()); }); $('#example1').datetimepicker('setDate', (new Date(2010, 11, 20, 16, 03))); }); </script> 

The correct date will be set, but not the time and hour and minute sliders. The time is also displayed as 00 03, not 16 03.

Can someone tell me what I'm doing wrong or not doing here so that the datetimepicker shows the correct default settings?

Other settings:

 $('#example1').datetimepicker({ dateFormat: 'dd mm yy', timeFormat: 'hh mm', showMinute: false, showSecond: false, separator: ' ', }); 

thanks

+4
source share
4 answers

I know this problem is outdated. But for those who are still looking for a solution, here's how I did it. The DatetimePicker plugin is an add-on for jQueryUI DatePicker, so I think all DatePicker parameters and methods are still applied in addition to the DatetimePicker.

So, if you have a text field with the identifier "dtp" and you want to initialize it with "5/31/2013 7:30 PM", you will do:

 $("#dtp").datetimepicker({ defaultDate: "5/31/2013", // this is from jQueryUI datepicker hour: 19, minute: 30 }); 

This should indicate the correct date in the calendar and set the sliders to the correct hours and minutes, as defined in the parameters.

Hope this helps.

+3
source

If you prefill the input field with a date value, datepicker should use this value by default.

In terms of usability, I would use this approach, not a more software solution.

+1
source

if you are talking about datetimepicker from Trent Richardson look at this topic fooobar.com/questions/1349786 / ...

In my experience, even if you have a date pre-populated in a text box, if you want to close the datepicker dialog box without entering a date, it replaces the value in the text box with today's. The solution is to set the value of your text field in the onClose method. In this example, the value will contain your original value:

  $('input.datetime').datetimepicker({ onClose: function (value) { $('input.datetime').val(value); } }); 
+1
source

The problem is with the settings of the date and time formats you are using. If you restore the original settings, the datetimepicker display should be correct.

0
source

All Articles