that_guy_jrb
that_guy_jrb

Reputation: 59

Changing To Date when From date is entered

I have a Fields. One is ValidFrom Date and the other is ValidTo Date.

My question is what should I do so that if ValidFrom date is entered using DateTimePicker, how should I change the ValidTo by exactly 30 days automatically?

Following is My code for DateTimePicker:

<script src="~/Scripts/jquery.datetimepicker.js"></script>
<script>
    $(document).ready(function () {
        $('#ValidFrom').datetimepicker({
            datepicker: true,
            timepicker: false,
            format: 'm/d/Y',
            step: 30,
            minDate: new Date(),
            onChangeDateTime: function (dp, $input) {
                var date = new Date($input.val());
                date.setMonth(date.getMonth() + 1);
                $('#ValidTo').datetimepicker({
                    datepicker: true,
                    timepicker: false,
                    format: 'm/d/Y',
                    step: 30,
                    minDate: date,
                });
            },

        });

    });
</script>

I have to change the ValidTo date by 30 days as and when ValidFrom date is entered.

Upvotes: 0

Views: 116

Answers (2)

that_guy_jrb
that_guy_jrb

Reputation: 59

This is how the code should be like: Got done with it after number of tries

<script>
    $(document).ready(function () {
        $('#ValidFrom').datetimepicker({
            datepicker: true,
            timepicker: false,
            format: 'm/d/Y',
            step: 30,
            minDate: new Date(),
            onChangeDateTime: function (dp, $input) {
                var date = new Date($input.val());
                date.setMonth(date.getMonth() + 1);
                date = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
                $('#ValidTo').val(date);
            },

        });
        $('#ValidTo').datetimepicker({
            datepicker: true,
            timepicker: false,
            format: 'm/d/Y',
            step: 30,
            minDate: new Date()
        });
    });

</script>

Upvotes: 1

Anant
Anant

Reputation: 534

The thing you're looking for is set under setDate() method of datetimepicker JS, it can take parameter as setDate(new Date()). You can fill your own date (format as required) in the argument of it. If you want no other date to get selected prior to your 30 days limit, you can check for start or startDate parameters in the same JS.

I suggest you to read datetimepicker.js that you're currently using to get to know about all of its features and parameters

Upvotes: 0

Related Questions