0

I am using this jQuery DateTimePicker plugin with my AngularJS app. I am implementing the plugin using directives in AngularJS. My problem is that I cannot find a way to put an upper and lower limit on the date/time picker that pops up.

Take a look at my code on Plunker and let me know what I am doing wrong.

Relevant code:

var updateDateTime = function(dateLower, dateUpper)
{
  if(!dateLower || !dateUpper)
    {
        return;
    }
    else if(!dateLower.val() || !dateUpper.val())
        return;
    else
    {
        if(dateUpper.val())
        {
            var dateStringUpper = dateUpper.val().split(' ');
            var maxDate = dateStringUpper[0] + ' ' + dateStringUpper[1] + ' ' + dateStringUpper[2];
            var maxTime = dateStringUpper[4] + ' ' + dateStringUpper[5];
            dateLower.setOptions({
                maxDate: maxDate,
                maxTime: maxTime
            });
        }
        if(dateLower.val())
        {
            var dateStringLower = dateLower.val().split(' ');
            var minDate = dateStringLower[0] + ' ' + dateStringLower[1] + ' ' + dateStringLower[2];
            var minTime = dateStringLower[4] + ' ' + dateStringLower[5];
            dateUpper.setOptions({
                minDate: minDate,
                minTime: minTime
            });
        }
    }
};

myApp.directive("ngDatetimestart", function($timeout) {
    return {
        require: 'ngModel',
        restrict: 'A',
        link: function(scope, elem, attrs, ngModel) {
            $timeout(function() {

                    $(elem).datetimepicker({
                        format: 'F j, Y @ g:i A',
                        formatDate: 'F j, Y',
                        formatTime: 'g:i A',
                        timepicker: true,
                        lang: 'en',
                        onShow: updateDateTime($(elem), $('#end_date_text')),
                        onChangeDateTime: updateDateTime($(elem), $('#end_date_text'))
                    });

                }, 0);
        }
    };
});

myApp.directive("ngDatetimeend", function($timeout) {
    return {
        require: 'ngModel',
        restrict: 'A',
        link: function(scope, elem, attrs, ngModel) {
            $timeout(function() {

                    $(elem).datetimepicker({
                        format: 'F j, Y @ g:i A',
                        formatDate: 'F j, Y',
                        formatTime: 'g:i A',
                        timepicker: true,
                        lang: 'en',
                        onShow: updateDateTime($('#start_date_text'), $(elem)),
                        onChangeDateTime: updateDateTime($('#start_date_text'), $(elem))
                    });

                }, 0);
        }
    };
});

Thanks!

1 Answer 1

2

The datetimepicker folks have a perfectly good example of this in their docs:

http://xdsoft.net/jqplugins/datetimepicker/#range

You have to be particularly careful of what exactly "this" resolves to in the onShow logic, as well as making sure to properly parse the text of the datetime input fields.

There were a few things missing or wrong in your code, but I've cleaned yours up a bit and have a working demo:

http://plnkr.co/edit/fu4MlBOlroQHmqvrhKTl

The relevant code:

myApp.directive("ngDatetimestart", function($timeout) {
    return {
        require: 'ngModel',
        restrict: 'A',
        link: function(scope, elem, attrs, ngModel) {
            $timeout(function() {

                    $(elem).datetimepicker({
                        format: 'F j, Y @ g:i A',
                        formatDate: 'F j, Y',
                        formatTime: 'g:i A',
                        timepicker: true,
                        lang: 'en',
                        onShow: function(ct) {
                          this.setOptions({
                            maxDate: $("#end_date_text").val() ? new Date(Date.parse($("#end_date_text").val())) : false
                          }); 
                        }
                    });

                }, 0);
        }
    };
});

myApp.directive("ngDatetimeend", function($timeout) {
    return {
        require: 'ngModel',
        restrict: 'A',
        link: function(scope, elem, attrs, ngModel) {
            $timeout(function() {

                    $(elem).datetimepicker({
                        format: 'F j, Y @ g:i A',
                        formatDate: 'F j, Y',
                        formatTime: 'g:i A',
                        timepicker: true,
                        lang: 'en',
                        onShow: function(ct) {
                          this.setOptions({
                            minDate: $("#start_date_text").val() ? new Date(Date.parse($("#start_date_text").val())) : false
                          }); 
                        }
                    });

                }, 0);
        }
    };
});
Sign up to request clarification or add additional context in comments.

5 Comments

This works well, thanks! I have tried to restrict the time as well (if the dates are the same) but could not get it to work. I also could not get this to work if I tried making the directives more "independent". What if I have two sets of start and end datetime boxes in the form. How would I go about working with these?
For the start and end times, use minTime and maxTime. I don't really know what you mean about "independent". If you wanted multiple sets, you'd just need to specify different selectors somehow.
I had this working how I wanted before adding it as a directive for AngularJS - so I'm aware of maxTime and minTime (I've read through the documentation). I'm just not very fluent with JavaScript and it would not have occurred to me to use something like Date(Date.parse($("#end_date_text").val())). For my other question, I'll try using the attrs parameter passed to the function to specify a different selector.
Take a look at my updated Plunk to see what I have tried to make the maxTime setting work.
Alright - this one works: plnkr.co/edit/aGfEdc8FsqC3heiDt9Nn?p=preview Please accept my answer if you feel like it answered the question that you asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.