@@ -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' , 'uibDateUtils' ,
25
+ function ( $scope , $attrs , $parse , $interpolate , $log , dateFilter , datepickerConfig , $datepickerSuppressError , dateUtils ) {
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 ? dateUtils . 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 ] ? dateUtils . 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 = dateUtils . 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 = dateUtils . 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 = dateUtils . 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 = dateUtils . 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 ? dateUtils . 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 = dateUtils . 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' , 'uibDateUtils' , 'uibDatepickerConfig' ,
556
+ function ( scope , element , attrs , $compile , $parse , $document , $rootScope , $position , dateFilter , dateParser , datepickerPopupConfig , $timeout , dateUtils , 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,10 @@ 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 = ngModelOptions ;
598
611
popupEl . attr ( {
599
612
'ng-model' : 'date' ,
613
+ 'ng-model-options' : 'ngModelOptions' ,
600
614
'ng-change' : 'dateSelection(date)' ,
601
615
'template-url' : datepickerPopupTemplateUrl
602
616
} ) ;
@@ -615,7 +629,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
615
629
if ( attrs . datepickerOptions ) {
616
630
var options = scope . $parent . $eval ( attrs . datepickerOptions ) ;
617
631
if ( options && options . initDate ) {
618
- scope . initDate = options . initDate ;
632
+ scope . initDate = dateUtils . fromTimezone ( options . initDate , ngModelOptions . timezone ) ;
619
633
datepickerEl . attr ( 'init-date' , 'initDate' ) ;
620
634
delete options . initDate ;
621
635
}
@@ -628,9 +642,12 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
628
642
if ( attrs [ key ] ) {
629
643
var getAttribute = $parse ( attrs [ key ] ) ;
630
644
scope . $parent . $watch ( getAttribute , function ( value ) {
631
- scope . watchData [ key ] = value ;
632
645
if ( key === 'minDate' || key === 'maxDate' ) {
633
- cache [ key ] = new Date ( value ) ;
646
+ cache [ key ] = dateUtils . fromTimezone ( new Date ( value ) , ngModelOptions . timezone ) ;
647
+ }
648
+ scope . watchData [ key ] = cache [ key ] || value ;
649
+ if ( key === 'initDate' ) {
650
+ scope . watchData [ key ] = dateUtils . fromTimezone ( new Date ( value ) , ngModelOptions . timezone ) ;
634
651
}
635
652
} ) ;
636
653
datepickerEl . attr ( cameltoDash ( key ) , 'watchData.' + key ) ;
@@ -667,8 +684,13 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
667
684
ngModel . $validators . date = validator ;
668
685
ngModel . $parsers . unshift ( parseDate ) ;
669
686
ngModel . $formatters . push ( function ( value ) {
670
- scope . date = value ;
671
- return ngModel . $isEmpty ( value ) ? value : dateFilter ( value , dateFormat ) ;
687
+ if ( ngModel . $isEmpty ( value ) ) {
688
+ scope . date = value ;
689
+ return value ;
690
+ }
691
+ var dt = dateUtils . fromTimezone ( value , ngModelOptions . timezone ) ;
692
+ scope . date = dt ;
693
+ return dateFilter ( dt , dateFormat ) ;
672
694
} ) ;
673
695
} else {
674
696
ngModel . $formatters . push ( function ( value ) {
@@ -830,7 +852,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
830
852
return undefined ;
831
853
}
832
854
833
- return date ;
855
+ return dateUtils . toTimezone ( date , ngModelOptions . timezone ) ;
834
856
}
835
857
836
858
return undefined ;
@@ -899,7 +921,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
899
921
900
922
. directive ( 'uibDatepickerPopup' , function ( ) {
901
923
return {
902
- require : [ 'ngModel ' , 'uibDatepickerPopup ' ] ,
924
+ require : [ 'uibDatepickerPopup ' , 'ngModel ' ] ,
903
925
controller : 'UibDatepickerPopupController' ,
904
926
scope : {
905
927
isOpen : '=?' ,
@@ -910,8 +932,8 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
910
932
customClass : '&'
911
933
} ,
912
934
link : function ( scope , element , attrs , ctrls ) {
913
- var ngModel = ctrls [ 0 ] ,
914
- ctrl = ctrls [ 1 ] ;
935
+ var ctrl = ctrls [ 0 ] ,
936
+ ngModel = ctrls [ 1 ] ;
915
937
916
938
ctrl . init ( ngModel ) ;
917
939
}
@@ -926,4 +948,40 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
926
948
return attrs . templateUrl || 'uib/template/datepicker/popup.html' ;
927
949
}
928
950
} ;
951
+ } )
952
+
953
+ . service ( 'uibDateUtils' , function ( ) {
954
+ return {
955
+ toTimezone : toTimezone ,
956
+ fromTimezone : fromTimezone ,
957
+ timezoneToOffset : timezoneToOffset ,
958
+ addDateMinutes : addDateMinutes ,
959
+ convertTimezoneToLocal : convertTimezoneToLocal
960
+ } ;
961
+
962
+ function toTimezone ( date , timezone ) {
963
+ return date && timezone ? convertTimezoneToLocal ( date , timezone ) : date ;
964
+ }
965
+
966
+ function fromTimezone ( date , timezone ) {
967
+ return date && timezone ? convertTimezoneToLocal ( date , timezone , true ) : date ;
968
+ }
969
+
970
+ //https://github.com/angular/angular.js/blob/4daafd3dbe6a80d578f5a31df1bb99c77559543e/src/Angular.js#L1207
971
+ function timezoneToOffset ( timezone , fallback ) {
972
+ var requestedTimezoneOffset = Date . parse ( 'Jan 01, 1970 00:00:00 ' + timezone ) / 60000 ;
973
+ return isNaN ( requestedTimezoneOffset ) ? fallback : requestedTimezoneOffset ;
974
+ }
975
+
976
+ function addDateMinutes ( date , minutes ) {
977
+ date = new Date ( date . getTime ( ) ) ;
978
+ date . setMinutes ( date . getMinutes ( ) + minutes ) ;
979
+ return date ;
980
+ }
981
+
982
+ function convertTimezoneToLocal ( date , timezone , reverse ) {
983
+ reverse = reverse ? - 1 : 1 ;
984
+ var timezoneOffset = timezoneToOffset ( timezone , date . getTimezoneOffset ( ) ) ;
985
+ return addDateMinutes ( date , reverse * ( timezoneOffset - date . getTimezoneOffset ( ) ) ) ;
986
+ }
929
987
} ) ;
0 commit comments