@@ -18,12 +18,15 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
18
18
yearColumns : 5 ,
19
19
minDate : null ,
20
20
maxDate : null ,
21
- shortcutPropagation : false
21
+ shortcutPropagation : false ,
22
+ ngModelOptions : { }
22
23
} )
23
24
24
- . controller ( 'UibDatepickerController' , [ '$scope' , '$attrs' , '$parse' , '$interpolate' , '$log' , 'dateFilter' , 'uibDatepickerConfig' , '$datepickerSuppressError' , function ( $scope , $attrs , $parse , $interpolate , $log , dateFilter , datepickerConfig , $datepickerSuppressError ) {
25
+ . controller ( 'UibDatepickerController' , [ '$scope' , '$attrs' , '$parse' , '$interpolate' , '$log' , 'dateFilter' , 'uibDatepickerConfig' , '$datepickerSuppressError' , 'uibDateParser' ,
26
+ function ( $scope , $attrs , $parse , $interpolate , $log , dateFilter , datepickerConfig , $datepickerSuppressError , dateParser ) {
25
27
var self = this ,
26
- ngModelCtrl = { $setViewValue : angular . noop } ; // nullModelCtrl;
28
+ ngModelCtrl = { $setViewValue : angular . noop } , // nullModelCtrl;
29
+ ngModelOptions = { } ;
27
30
28
31
// Modes chain
29
32
this . modes = [ 'day' , 'month' , 'year' ] ;
@@ -42,11 +45,11 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
42
45
angular . forEach ( [ 'minDate' , 'maxDate' ] , function ( key ) {
43
46
if ( $attrs [ key ] ) {
44
47
$scope . $parent . $watch ( $attrs [ key ] , function ( value ) {
45
- self [ key ] = value ? angular . isDate ( value ) ? new Date ( value ) : new Date ( dateFilter ( value , 'medium' ) ) : null ;
48
+ self [ key ] = value ? angular . isDate ( value ) ? dateParser . fromTimezone ( new Date ( value ) , ngModelOptions . timezone ) : new Date ( dateFilter ( value , 'medium' ) ) : null ;
46
49
self . refreshView ( ) ;
47
50
} ) ;
48
51
} else {
49
- self [ key ] = datepickerConfig [ key ] ? new Date ( datepickerConfig [ key ] ) : null ;
52
+ self [ key ] = datepickerConfig [ key ] ? dateParser . fromTimezone ( new Date ( datepickerConfig [ key ] ) , ngModelOptions . timezone ) : null ;
50
53
}
51
54
} ) ;
52
55
@@ -68,10 +71,10 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
68
71
$scope . uniqueId = 'datepicker-' + $scope . $id + '-' + Math . floor ( Math . random ( ) * 10000 ) ;
69
72
70
73
if ( angular . isDefined ( $attrs . initDate ) ) {
71
- this . activeDate = $scope . $parent . $eval ( $attrs . initDate ) || new Date ( ) ;
74
+ this . activeDate = dateParser . fromTimezone ( $scope . $parent . $eval ( $attrs . initDate ) , ngModelOptions . timezone ) || new Date ( ) ;
72
75
$scope . $parent . $watch ( $attrs . initDate , function ( initDate ) {
73
76
if ( initDate && ( ngModelCtrl . $isEmpty ( ngModelCtrl . $modelValue ) || ngModelCtrl . $invalid ) ) {
74
- self . activeDate = initDate ;
77
+ self . activeDate = dateParser . fromTimezone ( initDate , ngModelOptions . timezone ) ;
75
78
self . refreshView ( ) ;
76
79
}
77
80
} ) ;
@@ -97,6 +100,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
97
100
98
101
this . init = function ( ngModelCtrl_ ) {
99
102
ngModelCtrl = ngModelCtrl_ ;
103
+ ngModelOptions = ngModelCtrl_ . $options || datepickerConfig . ngModelOptions ;
100
104
101
105
if ( ngModelCtrl . $modelValue ) {
102
106
this . activeDate = ngModelCtrl . $modelValue ;
@@ -113,7 +117,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
113
117
isValid = ! isNaN ( date ) ;
114
118
115
119
if ( isValid ) {
116
- this . activeDate = date ;
120
+ this . activeDate = dateParser . fromTimezone ( date , ngModelOptions . timezone ) ;
117
121
} else if ( ! $datepickerSuppressError ) {
118
122
$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.' ) ;
119
123
}
@@ -126,13 +130,15 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
126
130
this . _refreshView ( ) ;
127
131
128
132
var date = ngModelCtrl . $viewValue ? new Date ( ngModelCtrl . $viewValue ) : null ;
133
+ date = dateParser . fromTimezone ( date , ngModelOptions . timezone ) ;
129
134
ngModelCtrl . $setValidity ( 'dateDisabled' , ! date ||
130
135
this . element && ! this . isDisabled ( date ) ) ;
131
136
}
132
137
} ;
133
138
134
139
this . createDateObject = function ( date , format ) {
135
140
var model = ngModelCtrl . $viewValue ? new Date ( ngModelCtrl . $viewValue ) : null ;
141
+ model = dateParser . fromTimezone ( model , ngModelOptions . timezone ) ;
136
142
return {
137
143
date : date ,
138
144
label : dateFilter ( date , format ) ,
@@ -165,8 +171,9 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
165
171
166
172
$scope . select = function ( date ) {
167
173
if ( $scope . datepickerMode === self . minMode ) {
168
- var dt = ngModelCtrl . $viewValue ? new Date ( ngModelCtrl . $viewValue ) : new Date ( 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ;
174
+ var dt = ngModelCtrl . $viewValue ? dateParser . fromTimezone ( new Date ( ngModelCtrl . $viewValue ) , ngModelOptions . timezone ) : new Date ( 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ;
169
175
dt . setFullYear ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) ;
176
+ dt = dateParser . toTimezone ( dt , ngModelOptions . timezone ) ;
170
177
ngModelCtrl . $setViewValue ( dt ) ;
171
178
ngModelCtrl . $render ( ) ;
172
179
} else {
@@ -550,19 +557,20 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
550
557
altInputFormats : [ ]
551
558
} )
552
559
553
- . controller ( 'UibDatepickerPopupController' , [ '$scope' , '$element' , '$attrs' , '$compile' , '$parse' , '$document' , '$rootScope' , '$uibPosition' , 'dateFilter' , 'uibDateParser' , 'uibDatepickerPopupConfig' , '$timeout' ,
554
- function ( scope , element , attrs , $compile , $parse , $document , $rootScope , $position , dateFilter , dateParser , datepickerPopupConfig , $timeout ) {
560
+ . controller ( 'UibDatepickerPopupController' , [ '$scope' , '$element' , '$attrs' , '$compile' , '$parse' , '$document' , '$rootScope' , '$uibPosition' , 'dateFilter' , 'uibDateParser' , 'uibDatepickerPopupConfig' , '$timeout' , 'uibDatepickerConfig' ,
561
+ function ( scope , element , attrs , $compile , $parse , $document , $rootScope , $position , dateFilter , dateParser , datepickerPopupConfig , $timeout , datepickerConfig ) {
555
562
var self = this ;
556
563
var cache = { } ,
557
564
isHtml5DateInput = false ;
558
565
var dateFormat , closeOnDateSelection , appendToBody , onOpenFocus ,
559
566
datepickerPopupTemplateUrl , datepickerTemplateUrl , popupEl , datepickerEl ,
560
- ngModel , $popup , altInputFormats ;
567
+ ngModel , ngModelOptions , $popup , altInputFormats ;
561
568
562
569
scope . watchData = { } ;
563
570
564
571
this . init = function ( _ngModel_ ) {
565
572
ngModel = _ngModel_ ;
573
+ ngModelOptions = _ngModel_ . $options || datepickerConfig . ngModelOptions ;
566
574
closeOnDateSelection = angular . isDefined ( attrs . closeOnDateSelection ) ? scope . $parent . $eval ( attrs . closeOnDateSelection ) : datepickerPopupConfig . closeOnDateSelection ;
567
575
appendToBody = angular . isDefined ( attrs . datepickerAppendToBody ) ? scope . $parent . $eval ( attrs . datepickerAppendToBody ) : datepickerPopupConfig . appendToBody ;
568
576
onOpenFocus = angular . isDefined ( attrs . onOpenFocus ) ? scope . $parent . $eval ( attrs . onOpenFocus ) : datepickerPopupConfig . onOpenFocus ;
@@ -602,8 +610,11 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
602
610
603
611
// popup element used to display calendar
604
612
popupEl = angular . element ( '<div uib-datepicker-popup-wrap><div uib-datepicker></div></div>' ) ;
613
+ scope . ngModelOptions = angular . copy ( ngModelOptions ) ;
614
+ scope . ngModelOptions . timezone = null ;
605
615
popupEl . attr ( {
606
616
'ng-model' : 'date' ,
617
+ 'ng-model-options' : 'ngModelOptions' ,
607
618
'ng-change' : 'dateSelection(date)' ,
608
619
'template-url' : datepickerPopupTemplateUrl
609
620
} ) ;
@@ -622,7 +633,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
622
633
if ( attrs . datepickerOptions ) {
623
634
var options = scope . $parent . $eval ( attrs . datepickerOptions ) ;
624
635
if ( options && options . initDate ) {
625
- scope . initDate = options . initDate ;
636
+ scope . initDate = dateParser . fromTimezone ( options . initDate , ngModelOptions . timezone ) ;
626
637
datepickerEl . attr ( 'init-date' , 'initDate' ) ;
627
638
delete options . initDate ;
628
639
}
@@ -636,9 +647,12 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
636
647
var getAttribute = $parse ( attrs [ key ] ) ;
637
648
scope . $parent . $watch ( getAttribute , function ( value ) {
638
649
if ( key === 'minDate' || key === 'maxDate' ) {
639
- cache [ key ] = angular . isDate ( value ) ? new Date ( value ) : new Date ( dateFilter ( value , 'medium' ) ) ;
650
+ cache [ key ] = angular . isDate ( value ) ? dateParser . fromTimezone ( new Date ( value ) , ngModelOptions . timezone ) : new Date ( dateFilter ( value , 'medium' ) ) ;
640
651
}
641
652
scope . watchData [ key ] = cache [ key ] || value ;
653
+ if ( key === 'initDate' ) {
654
+ scope . watchData [ key ] = dateParser . fromTimezone ( new Date ( value ) , ngModelOptions . timezone ) ;
655
+ }
642
656
} ) ;
643
657
datepickerEl . attr ( cameltoDash ( key ) , 'watchData.' + key ) ;
644
658
@@ -674,12 +688,16 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
674
688
ngModel . $validators . date = validator ;
675
689
ngModel . $parsers . unshift ( parseDate ) ;
676
690
ngModel . $formatters . push ( function ( value ) {
677
- scope . date = value ;
678
- return ngModel . $isEmpty ( value ) ? value : dateFilter ( value , dateFormat ) ;
691
+ if ( ngModel . $isEmpty ( value ) ) {
692
+ scope . date = value ;
693
+ return value ;
694
+ }
695
+ scope . date = dateParser . fromTimezone ( value , ngModelOptions . timezone ) ;
696
+ return dateFilter ( scope . date , dateFormat ) ;
679
697
} ) ;
680
698
} else {
681
699
ngModel . $formatters . push ( function ( value ) {
682
- scope . date = value ;
700
+ scope . date = dateParser . fromTimezone ( value , ngModelOptions . timezone ) ;
683
701
return value ;
684
702
} ) ;
685
703
}
@@ -835,7 +853,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
835
853
if ( angular . isString ( viewValue ) ) {
836
854
var date = parseDateString ( viewValue ) ;
837
855
if ( ! isNaN ( date ) ) {
838
- return date ;
856
+ return dateParser . toTimezone ( date , ngModelOptions . timezone ) ;
839
857
}
840
858
}
841
859
0 commit comments