最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery Ui date time picker - Stack Overflow

programmeradmin2浏览0评论

I am using jquery date time picker and using start date and end date.

When selecting same date for start date and end date I do not want it to allow the end date time to be less than start date time. How would I adjust my code?

$( "#fdate" ).datetimepicker({
     dateFormat: 'yy-mm-dd',timeFormat: 'HH:mm:ss',
        onClose: function( selectedDate ) {
        $( "#tdate" ).datetimepicker( "option", "minDate", selectedDate );
  }
  }).attr('readonly', 'readonly');
     $( "#tdate" ).datetimepicker({dateFormat: 'yy-mm-dd',timeFormat: 'HH:mm:ss',
  onClose: function( selectedDate ) {
     $( "#fdate" ).datetimepicker( "option", "maxDate", selectedDate );
  }
  }).attr('readonly', 'readonly');
});

here fdate is start date and tdate is end date and here is Fiddle of the issue

I am using jquery date time picker and using start date and end date.

When selecting same date for start date and end date I do not want it to allow the end date time to be less than start date time. How would I adjust my code?

$( "#fdate" ).datetimepicker({
     dateFormat: 'yy-mm-dd',timeFormat: 'HH:mm:ss',
        onClose: function( selectedDate ) {
        $( "#tdate" ).datetimepicker( "option", "minDate", selectedDate );
  }
  }).attr('readonly', 'readonly');
     $( "#tdate" ).datetimepicker({dateFormat: 'yy-mm-dd',timeFormat: 'HH:mm:ss',
  onClose: function( selectedDate ) {
     $( "#fdate" ).datetimepicker( "option", "maxDate", selectedDate );
  }
  }).attr('readonly', 'readonly');
});

here fdate is start date and tdate is end date and here is Fiddle of the issue

Share Improve this question edited May 22, 2015 at 13:54 JasonWilczak 2,4032 gold badges22 silver badges39 bronze badges asked May 22, 2015 at 12:41 Srinath MurugulaSrinath Murugula 5821 gold badge9 silver badges30 bronze badges 2
  • And what is the question? – briosheje Commented May 22, 2015 at 12:44
  • we shouldnot able to select end date time less than start date time – Srinath Murugula Commented May 22, 2015 at 12:47
Add a ment  | 

1 Answer 1

Reset to default 3

You need to use the 'setOptions' method for each timepicker:

$(document).ready(function(){
    $("#fdate" ).datetimepicker({
        dateFormat: 'yy-mm-dd',
        timeFormat: 'HH:mm:ss',
        onShow: function () {
            this.setOptions({
                maxDate:$('#tdate').val()?$('#tdate').val():false,
                maxTime:$('#tdate').val()?$('#tdate').val():false
            });
        }
  }).attr('readonly', 'readonly');
  $( "#tdate" ).datetimepicker({
      dateFormat: 'yy-mm-dd',
      timeFormat: 'HH:mm:ss',
        onShow: function () {
            this.setOptions({
                minDate:$('#fdate').val()?$('#fdate').val():false,
                minTime:$('#fdate').val()?$('#fdate').val():false
            });
        }                    
  }).attr('readonly', 'readonly');    

Here is the Modified Fiddle.

This is not perfect, but it will get you close, you may need to handle some of the formatting. I used the 'onShow' event, but you could easily do it in the onClose, as well. There is a small sample on their official page called Range between date#.

发布评论

评论列表(0)

  1. 暂无评论