@@ -17,12 +17,15 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
17
17
yearRange : 20 ,
18
18
minDate : null ,
19
19
maxDate : null ,
20
- shortcutPropagation : false
20
+ shortcutPropagation : false ,
21
+ ngModelOptions : { }
21
22
} )
22
23
23
- . controller ( 'UibDatepickerController' , [ '$scope' , '$attrs' , '$parse' , '$interpolate' , '$log' , 'dateFilter' , 'uibDatepickerConfig' , '$datepickerSuppressError' , function ( $scope , $attrs , $parse , $interpolate , $log , dateFilter , datepickerConfig , $datepickerSuppressError ) {
24
+ . controller ( 'UibDatepickerController' , [ '$scope' , '$attrs' , '$parse' , '$interpolate' , '$log' , 'dateFilter' , 'uibDatepickerConfig' , '$datepickerSuppressError' , 'uibDateParser' ,
25
+ function ( $scope , $attrs , $parse , $interpolate , $log , dateFilter , datepickerConfig , $datepickerSuppressError , dateParser ) {
24
26
var self = this ,
25
- ngModelCtrl = { $setViewValue : angular . noop } ; // nullModelCtrl;
27
+ ngModelCtrl = { $setViewValue : angular . noop } , // nullModelCtrl;
28
+ ngModelOptions = { } ;
26
29
27
30
// Modes chain
28
31
this . modes = [ 'day' , 'month' , 'year' ] ;
@@ -41,11 +44,11 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
41
44
angular . forEach ( [ 'minDate' , 'maxDate' ] , function ( key ) {
42
45
if ( $attrs [ key ] ) {
43
46
$scope . $parent . $watch ( $attrs [ key ] , function ( value ) {
44
- self [ key ] = value ? new Date ( value ) : null ;
47
+ self [ key ] = value ? dateParser . fromTimezone ( new Date ( value ) , ngModelOptions . timezone ) : null ;
45
48
self . refreshView ( ) ;
46
49
} ) ;
47
50
} else {
48
- self [ key ] = datepickerConfig [ key ] ? new Date ( datepickerConfig [ key ] ) : null ;
51
+ self [ key ] = datepickerConfig [ key ] ? dateParser . fromTimezone ( new Date ( datepickerConfig [ key ] ) , ngModelOptions . timezone ) : null ;
49
52
}
50
53
} ) ;
51
54
@@ -70,7 +73,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
70
73
this . activeDate = $scope . $parent . $eval ( $attrs . initDate ) || new Date ( ) ;
71
74
$scope . $parent . $watch ( $attrs . initDate , function ( initDate ) {
72
75
if ( initDate && ( ngModelCtrl . $isEmpty ( ngModelCtrl . $modelValue ) || ngModelCtrl . $invalid ) ) {
73
- self . activeDate = initDate ;
76
+ self . activeDate = dateParser . fromTimezone ( initDate , ngModelOptions . timezone ) ;
74
77
self . refreshView ( ) ;
75
78
}
76
79
} ) ;
@@ -96,6 +99,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
96
99
97
100
this . init = function ( ngModelCtrl_ ) {
98
101
ngModelCtrl = ngModelCtrl_ ;
102
+ ngModelOptions = ngModelCtrl_ . $options || datepickerConfig . ngModelOptions ;
99
103
100
104
ngModelCtrl . $render = function ( ) {
101
105
self . render ( ) ;
@@ -108,7 +112,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
108
112
isValid = ! isNaN ( date ) ;
109
113
110
114
if ( isValid ) {
111
- this . activeDate = date ;
115
+ this . activeDate = dateParser . fromTimezone ( date , ngModelOptions . timezone ) ;
112
116
} else if ( ! $datepickerSuppressError ) {
113
117
$log . error ( 'Datepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.' ) ;
114
118
}
@@ -121,13 +125,15 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
121
125
this . _refreshView ( ) ;
122
126
123
127
var date = ngModelCtrl . $viewValue ? new Date ( ngModelCtrl . $viewValue ) : null ;
128
+ date = dateParser . fromTimezone ( date , ngModelOptions . timezone ) ;
124
129
ngModelCtrl . $setValidity ( 'dateDisabled' , ! date ||
125
130
this . element && ! this . isDisabled ( date ) ) ;
126
131
}
127
132
} ;
128
133
129
134
this . createDateObject = function ( date , format ) {
130
135
var model = ngModelCtrl . $viewValue ? new Date ( ngModelCtrl . $viewValue ) : null ;
136
+ model = dateParser . fromTimezone ( model , ngModelOptions . timezone ) ;
131
137
return {
132
138
date : date ,
133
139
label : dateFilter ( date , format ) ,
@@ -160,8 +166,9 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
160
166
161
167
$scope . select = function ( date ) {
162
168
if ( $scope . datepickerMode === self . minMode ) {
163
- var dt = ngModelCtrl . $viewValue ? new Date ( ngModelCtrl . $viewValue ) : new Date ( 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ;
169
+ var dt = ngModelCtrl . $viewValue ? dateParser . fromTimezone ( new Date ( ngModelCtrl . $viewValue ) , ngModelOptions . timezone ) : new Date ( 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ;
164
170
dt . setFullYear ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) ;
171
+ dt = dateParser . toTimezone ( dt , ngModelOptions . timezone ) ;
165
172
ngModelCtrl . $setViewValue ( dt ) ;
166
173
ngModelCtrl . $render ( ) ;
167
174
} else {
@@ -465,7 +472,9 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
465
472
controller : 'UibDatepickerController' ,
466
473
controllerAs : 'datepicker' ,
467
474
link : function ( scope , element , attrs , ctrls ) {
468
- var datepickerCtrl = ctrls [ 0 ] , ngModelCtrl = ctrls [ 1 ] ;
475
+ var datepickerCtrl = ctrls [ 0 ] ,
476
+ ngModelCtrl = ctrls [ 1 ] ;
477
+
469
478
470
479
datepickerCtrl . init ( ngModelCtrl ) ;
471
480
}
@@ -543,19 +552,20 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
543
552
altInputFormats : [ ]
544
553
} )
545
554
546
- . controller ( 'UibDatepickerPopupController' , [ '$scope' , '$element' , '$attrs' , '$compile' , '$parse' , '$document' , '$rootScope' , '$uibPosition' , 'dateFilter' , 'uibDateParser' , 'uibDatepickerPopupConfig' , '$timeout' ,
547
- function ( scope , element , attrs , $compile , $parse , $document , $rootScope , $position , dateFilter , dateParser , datepickerPopupConfig , $timeout ) {
555
+ . controller ( 'UibDatepickerPopupController' , [ '$scope' , '$element' , '$attrs' , '$compile' , '$parse' , '$document' , '$rootScope' , '$uibPosition' , 'dateFilter' , 'uibDateParser' , 'uibDatepickerPopupConfig' , '$timeout' , 'uibDatepickerConfig' ,
556
+ function ( scope , element , attrs , $compile , $parse , $document , $rootScope , $position , dateFilter , dateParser , datepickerPopupConfig , $timeout , datepickerConfig ) {
548
557
var self = this ;
549
558
var cache = { } ,
550
559
isHtml5DateInput = false ;
551
560
var dateFormat , closeOnDateSelection , appendToBody , onOpenFocus ,
552
561
datepickerPopupTemplateUrl , datepickerTemplateUrl , popupEl , datepickerEl ,
553
- ngModel , $popup , altInputFormats ;
562
+ ngModel , ngModelOptions , $popup , altInputFormats ;
554
563
555
564
scope . watchData = { } ;
556
565
557
566
this . init = function ( _ngModel_ ) {
558
567
ngModel = _ngModel_ ;
568
+ ngModelOptions = _ngModel_ . $options || datepickerConfig . ngModelOptions ;
559
569
closeOnDateSelection = angular . isDefined ( attrs . closeOnDateSelection ) ? scope . $parent . $eval ( attrs . closeOnDateSelection ) : datepickerPopupConfig . closeOnDateSelection ;
560
570
appendToBody = angular . isDefined ( attrs . datepickerAppendToBody ) ? scope . $parent . $eval ( attrs . datepickerAppendToBody ) : datepickerPopupConfig . appendToBody ;
561
571
onOpenFocus = angular . isDefined ( attrs . onOpenFocus ) ? scope . $parent . $eval ( attrs . onOpenFocus ) : datepickerPopupConfig . onOpenFocus ;
@@ -568,6 +578,8 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
568
578
if ( datepickerPopupConfig . html5Types [ attrs . type ] ) {
569
579
dateFormat = datepickerPopupConfig . html5Types [ attrs . type ] ;
570
580
isHtml5DateInput = true ;
581
+ ngModelOptions . timezoneHtml5 = ngModelOptions . timezone ;
582
+ ngModelOptions . timezone = null ;
571
583
} else {
572
584
dateFormat = attrs . uibDatepickerPopup || datepickerPopupConfig . datepickerPopup ;
573
585
attrs . $observe ( 'uibDatepickerPopup' , function ( value , oldValue ) {
@@ -595,8 +607,11 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
595
607
596
608
// popup element used to display calendar
597
609
popupEl = angular . element ( '<div uib-datepicker-popup-wrap><div uib-datepicker></div></div>' ) ;
610
+ scope . ngModelOptions = angular . copy ( ngModelOptions ) ;
611
+ scope . ngModelOptions . timezone = null ;
598
612
popupEl . attr ( {
599
613
'ng-model' : 'date' ,
614
+ 'ng-model-options' : 'ngModelOptions' ,
600
615
'ng-change' : 'dateSelection(date)' ,
601
616
'template-url' : datepickerPopupTemplateUrl
602
617
} ) ;
@@ -615,7 +630,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
615
630
if ( attrs . datepickerOptions ) {
616
631
var options = scope . $parent . $eval ( attrs . datepickerOptions ) ;
617
632
if ( options && options . initDate ) {
618
- scope . initDate = options . initDate ;
633
+ scope . initDate = dateParser . fromTimezone ( options . initDate , ngModelOptions . timezone || ngModelOptions . timezoneHtml5 ) ;
619
634
datepickerEl . attr ( 'init-date' , 'initDate' ) ;
620
635
delete options . initDate ;
621
636
}
@@ -628,9 +643,12 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
628
643
if ( attrs [ key ] ) {
629
644
var getAttribute = $parse ( attrs [ key ] ) ;
630
645
scope . $parent . $watch ( getAttribute , function ( value ) {
631
- scope . watchData [ key ] = value ;
632
646
if ( key === 'minDate' || key === 'maxDate' ) {
633
- cache [ key ] = new Date ( value ) ;
647
+ cache [ key ] = dateParser . fromTimezone ( new Date ( value ) , ngModelOptions . timezone ) ;
648
+ }
649
+ scope . watchData [ key ] = cache [ key ] || value ;
650
+ if ( key === 'initDate' ) {
651
+ scope . watchData [ key ] = dateParser . fromTimezone ( new Date ( value ) , ngModelOptions . timezone || ngModelOptions . timezoneHtml5 ) ;
634
652
}
635
653
} ) ;
636
654
datepickerEl . attr ( cameltoDash ( key ) , 'watchData.' + key ) ;
@@ -667,12 +685,18 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
667
685
ngModel . $validators . date = validator ;
668
686
ngModel . $parsers . unshift ( parseDate ) ;
669
687
ngModel . $formatters . push ( function ( value ) {
670
- scope . date = value ;
671
- return ngModel . $isEmpty ( value ) ? value : dateFilter ( value , dateFormat ) ;
688
+ if ( ngModel . $isEmpty ( value ) ) {
689
+ scope . date = value ;
690
+ return value ;
691
+ }
692
+ var dt = dateParser . fromTimezone ( value , ngModelOptions . timezone ) ;
693
+ scope . date = dt ;
694
+ return dateFilter ( dt , dateFormat ) ;
672
695
} ) ;
673
696
} else {
674
697
ngModel . $formatters . push ( function ( value ) {
675
- scope . date = value ;
698
+ var dt = dateParser . fromTimezone ( value , ngModelOptions . timezoneHtml5 ) ;
699
+ scope . date = dt ;
676
700
return value ;
677
701
} ) ;
678
702
}
@@ -731,7 +755,8 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
731
755
if ( angular . isDefined ( dt ) ) {
732
756
scope . date = dt ;
733
757
}
734
- var date = scope . date ? dateFilter ( scope . date , dateFormat ) : null ; // Setting to NULL is necessary for form validators to function
758
+ // Setting to NULL is necessary for form validators to function
759
+ var date = scope . date ? dateFilter ( scope . date , dateFormat ) : null ;
735
760
element . val ( date ) ;
736
761
ngModel . $setViewValue ( date ) ;
737
762
@@ -827,7 +852,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
827
852
if ( angular . isString ( viewValue ) ) {
828
853
var date = parseDateString ( viewValue ) ;
829
854
if ( ! isNaN ( date ) ) {
830
- return date ;
855
+ return dateParser . toTimezone ( date , ngModelOptions . timezone ) ;
831
856
}
832
857
}
833
858
@@ -897,7 +922,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
897
922
898
923
. directive ( 'uibDatepickerPopup' , function ( ) {
899
924
return {
900
- require : [ 'ngModel ' , 'uibDatepickerPopup ' ] ,
925
+ require : [ 'uibDatepickerPopup ' , 'ngModel ' ] ,
901
926
controller : 'UibDatepickerPopupController' ,
902
927
scope : {
903
928
isOpen : '=?' ,
@@ -908,8 +933,8 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
908
933
customClass : '&'
909
934
} ,
910
935
link : function ( scope , element , attrs , ctrls ) {
911
- var ngModel = ctrls [ 0 ] ,
912
- ctrl = ctrls [ 1 ] ;
936
+ var ctrl = ctrls [ 0 ] ,
937
+ ngModel = ctrls [ 1 ] ;
913
938
914
939
ctrl . init ( ngModel ) ;
915
940
}
0 commit comments