0

I'm totally new to angular js. But I somehow managed to integrate a jQuery UI datepicker in my Angular JS project. But now I want to format the date picked by the jQuery datepicker into ISO date format which I'm not being able to do. Here is my code.

HTML

<input type="text" ng-model="event.when.start" datetime-picker>

ANGULAR Controller

myApp.directive('datetimePicker', function() {
   return {
        require : 'ngModel',
        link : function (scope, element, attrs, ngModel) {
            $(function(){
                element.datetimepicker({
                    dateFormat:'yy-mm-dd',
                    minDate: 0,
                    yearRange: '1920:2012',
                    minute: 0,
                    stepMinute: 15,
                    onSelect:function (dateText, inst) {
                        var dt = new Date(dateText);
                        alert(dt.toISOString());
                        ngModel.$setViewValue(dt.toISOString());
                        scope.$apply();
                    }
                });
            });
        }
    }
});

SCREENSHOT

enter image description here

Note*: I'm being able to alert the value though.

1

1 Answer 1

2

simply as below

myapp.controller('myctrl',function myctrl ($scope){
    $scope.event = {when : {start : ''}}
    $scope.event.when.start = new Date();
}).

    myApp.directive('datetimePicker', function() {
       return {
            require : 'ngModel',
            link : function (scope, element, attrs, ngModel) {
                $(function(){
                    element.datetimepicker({
                        minDate: 0,
                        yearRange: '1920:2012',
                        minute: 0,
                        stepMinute: 15,
                        onSelect:function (dateText, inst) {

                            scope.$apply(function(){
                               scope.event.when.start = new Date(dateText);
                            });
                        }
                    });
                });
            }
        }
    });

don't use dateFormat:'yy-mm-dd', if you need ISO Date

Sign up to request clarification or add additional context in comments.

6 Comments

thank you for the answer @bews99. It works! :) but for that i need to click twice on the datepicker. Is it possible to format the date when i focus on that input field.
I edited my comment above. As i said, it is working but i need to click twice on the date of datepicker, to make it work.
my project requires lots of other js and css. I'm not being able to put it in jsfiddle. is there any other way?
updated answer.check wrap your code inside scope.$apply
still not working as required @bews99 :( Actually, when i focus on input field, the datepicker automatically fills up the field with the default now time. Which I want it to be filled with ISO formatted datetime. Could you please help me with that. I heartly appreciate your help.
|

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.