DateTime Standalone - how to update minDate

DateTime Standalone - how to update minDate

tomduketomduke Posts: 13Questions: 2Answers: 0

Hi,

I want to update the minDate for a second DateTime field based on the selected date of a first DateTime field.

I can update the value of the second field using this:

var dd1 = new DateTime(document.getElementById('departure_date_1'), {
    format: 'DD/MM/YYYY HH:mm',
    onChange: function (value, date, input) {
        ad1.val(date);
    }
});

var ad1 = new DateTime(document.getElementById('arrival_date_1'), {
    format: 'DD/MM/YYYY HH:mm',
}); 

But I can't figure out how to update the minDate, this does not work:

var dd1 = new DateTime(document.getElementById('departure_date_1'), {
    format: 'DD/MM/YYYY HH:mm',
    onChange: function (value, date, input) {
        ad1.val(date);
    }
});

var ad1 = new DateTime(document.getElementById('arrival_date_1'), {
    format: 'DD/MM/YYYY HH:mm',
}); 

Any ideas?

Thanks
- Tom

This question has an accepted answers - jump to answer

Answers

  • tomduketomduke Posts: 13Questions: 2Answers: 0

    Sorry, I meant this does not work:

    var dd1 = new DateTime(document.getElementById('departure_date_1'), {
        format: 'DD/MM/YYYY HH:mm',
        onChange: function (value, date, input) {
            ad1.minDate(date);
        }
    });
    
    var ad1 = new DateTime(document.getElementById('arrival_date_1'), {
        format: 'DD/MM/YYYY HH:mm',
    }); 
    
    • Tom
  • kthorngrenkthorngren Posts: 22,162Questions: 26Answers: 5,102

    I'm not sure why the DateTime API docs don't seem to be available. @allan will need to look at this.

    I opened this basic example and checked the API:

    It looks like the API is min() not minDate(). Try using ad1.min(date); and let us know the results.

    Kevin

  • allanallan Posts: 64,756Questions: 1Answers: 10,716 Site admin

    Looks like I've messed up my nginx config for that path - sorry. I'll look into that. In the meantime, if you go directly to https://datatables.net/extensions/datetime/api/ it will load the API methods, which as Kevin notes, will be min() in this case.

    Allan

  • tomduketomduke Posts: 13Questions: 2Answers: 0

    Hi,

    That's working now thanks when using min()

    var dd1 = new DateTime(document.getElementById('departure_date_1'), {
        format: 'DD/MM/YYYY HH:mm',
        onChange: function (value, date, input) {
            ad1.min(date);
        }
    });
    
    var ad1 = new DateTime(document.getElementById('arrival_date_1'), {
        format: 'DD/MM/YYYY HH:mm',
    });
    

    A follow on question, can I get the second 'arrival_date_1' field to now display the minDate when opened rather than today? I was hoping this might work:

    ad1.display( date.getFullYear(), date.getMonth() );
    

    But it is throwing an error.

    Thanks
    - Tom

  • kthorngrenkthorngren Posts: 22,162Questions: 26Answers: 5,102
    Answer ✓

    What error are you getting?

    Where are you using ad1.display( date.getFullYear(), date.getMonth() );?

    I copied your code into this test case and tried using the display() API in the onChange event and got this error:

    TypeError: Cannot read properties of null (reading 'setUTCFullYear')

    I bit of debugging and it looks like the Datetime input for arrival_date_1 has not fully initialized:

    I removed the onChange event and created this click event:

    $('#arrival_date_1').on('click', function () {
      var date = dd1.val();
      
      ad1.min(date);
    
      // getMonth() returns 0 based month, ie Jan = 0
      ad1.display( date.getFullYear(), date.getMonth() + 1 );
    });
    

    Here is the test case:
    https://live.datatables.net/vicomohe/1/edit

    I think it does what you are looking for. If not please update the test case to show the issue you are having.

    Kevin

  • tomduketomduke Posts: 13Questions: 2Answers: 0

    Kevin,

    Hi - yes I was getting that same error:

    TypeError: Cannot read properties of null (reading 'setUTCFullYear')
    

    Your solution is great and gives me exactly what I was looking for.

    Thanks
    - Tom

  • allanallan Posts: 64,756Questions: 1Answers: 10,716 Site admin

    Just to follow up - the link for the API docs for DateTime is working now.

    Allan

Sign In or Register to comment.