datepicker minDate

datepicker minDate

montoyammontoyam Posts: 568Questions: 136Answers: 5

I need the minDate option to depend on a value in another datefield. I read that minDate won't accept a function, so what I have seen in non-DataTable projects is code like this:

$('[name=myradiogroup]').change(function () {
    var newMininumDate = /*insert logic here*/;
    $('#datepicker').datepicker('option', 'minDate', newMininumDate);
});

One way I was thinking of doing this in DataTables is to use the .clear() and .add() on open of the Editor (or select of the DataTable), like this:

var newMininumDate = /*insert logic here*/;
editor.add( {
    type:  'datetime',
    label: 'Start date:',
    name:  'start_date',
    opts:  {
        minDate :newMininumDate 
    }
} );

I just wanted to make sure this is the most efficient way to do this?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    Yep, that's a good way to go with that - and the only real solution for that use case,

    Colin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    thanks. here is my complete code in case anyone has the same type of need :)

                FundOrgDeptGroupEditor.on('open', function () {
                    var effectiveDate = FundOrgDeptGroupEditor.get("FundOrgDeptGroup_Header.effectivedate");
                    FundOrgDeptGroupEditor
                        .clear('FundOrgDeptGroup_Header.expiredate')
                        .add({
                                type: 'date',
                                label: 'Expire Date:',
                                name: 'FundOrgDeptGroup_Header.expiredate',
                                opts: {
                                    changeMonth: true,
                                    changeYear: true,
                                    showButtonPanel: true,
                                    dateFormat: 'm/d/yy',
                                    minDate: effectiveDate,
                                    onClose: function (dateText, inst) {
                                        var eom = new Date((new Date(inst.selectedYear, inst.selectedMonth + 1, 1)) - 1).getDate();
                                        $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, eom));
                                    }
                                }
                        });
                });
    
  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    a co-worker did this in what seems to be a cleaner way:

       SLAeditor.on('open', function (e, data, action) {
                        if (action == 'edit') {
                            SLAeditor.field('EarlyTerminationDate').minDate(new Date(SLAeditor.get("TermStartDate")));
                            SLAeditor.field('EarlyTerminationDate').maxDate(new Date(SLAeditor.get("TermEndDate")));
                        }
                    });
    
  • lopezmirlopezmir Posts: 1Questions: 0Answers: 1
    Answer ✓

    Thanks for posting. Here's what I tried:

       SLAeditor.on('open', function (e, data, action) {                   
                           
                SLAeditor.field('EarlyTerminationDate').minDate(new Date(SLAeditor.get("TermStartDate")));
                SLAeditor.field('EarlyTerminationDate').maxDate(new Date(SLAeditor.get("TermEndDate")));
                        }
                    });
    
  • allanallan Posts: 61,618Questions: 1Answers: 10,089 Site admin

    Agreed - if you want to dynamically set the minDate, then using the field().minDate() method for the datetime input would be the way to do it rather than removing and adding the field each time.

    Allan

  • bruce.e.scottbruce.e.scott Posts: 11Questions: 2Answers: 0
    edited October 2021

    From: Bruce Scott
    This is working for me!

      {
                    label: "Deliver Need Date:",
                    name: "DeliverNeedDate",
                    type: 'datetime',
                    disableDays:[1,0],
                    minDate: new Date('2021-10-06'),
                    //def: function () { return new Date(); },
    
                    displayFormat: 'MM-DD-YYYY',
                    keyInput: false,
                    opts:  {
                        showWeekNumber: true,
                        disableDays:[1,0],
                        minDate: new Date(new Date().toISOString().substring(0, 10)),
                        
                    }
    
Sign In or Register to comment.