jQuery Datepicker show old date when click on now button

63 Views Asked by At

When I click on 'Now' button its shows me correct date time only first time. but when click 2nd time or more it shows wrong (some old date time value) enter image description here

This my working code

$('.from-date-picker').datetimepicker({
  dateFormat: "dd-M-yy",
  timeFormat: "hh:mm tt",
  showOn: "both",
  timeInput: true,
  stepHour: 1,
  stepMinute: 5,
  oneLine: true,
  //changeMonth: true,
  //changeYear: true,
  controlType: 'select',
  buttonImageOnly: true,
  buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
  buttonText: "Select date"
});
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"> 
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.1/jquery-ui-sliderAccess.js"></script> -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.1/jquery-ui-timepicker-addon.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.1/jquery-ui-timepicker-addon.js"></script>

<input type="text" autocomplete="off" class="form-control from-date-picker" name="SaleOrder.Date" id="SaleOrderDate" readonly>

1

There are 1 best solutions below

2
Twisty On

When I run the following code, the date and time change on the first click to the correct value. When I click on it a second time, the date will sometimes revert to 12:00.

$(function() {
  $('.from-date-picker').datetimepicker({
    dateFormat: "dd-M-yy",
    timeFormat: "hh:mm tt",
    showOn: "both",
    timeInput: true,
    stepHour: 1,
    stepMinute: 5,
    oneLine: true,
    controlType: 'select',
    buttonImageOnly: true,
    buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
    buttonText: "Select date"
  });
});
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.1/jquery-ui-timepicker-addon.css">

<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.1/jquery-ui-timepicker-addon.js"></script>

<input type="text" autocomplete="off" class="form-control from-date-picker" name="SaleOrder.Date" id="SaleOrderDate" readonly>

Tested with more modern versions.

$(function() {
  $('.from-date-picker').datetimepicker({
    dateFormat: "dd-M-yy",
    timeFormat: "hh:mm tt",
    showOn: "both",
    timeInput: true,
    stepHour: 1,
    stepMinute: 5,
    oneLine: true,
    controlType: 'select',
    buttonImageOnly: true,
    buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
    buttonText: "Select date"
  });
});
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.3/jquery-ui-timepicker-addon.min.css">

<script type="text/javascript" src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.3/jquery-ui-timepicker-addon.min.js"></script>

<input type="text" autocomplete="off" class="form-control from-date-picker" name="SaleOrder.Date" id="SaleOrderDate" readonly>

It is now working as expected. So this may have been a Bug in the older timepicker code.