Skip to content

Commit 8935ff7

Browse files
committed
feat(modelType): Add support to moment-timezone#defaultZone
1 parent ab56b15 commit 8935ff7

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"karma-threshold-reporter": "^0.1.12",
5959
"lodash": "^4.5.0",
6060
"matchdep": "^1.0.1",
61+
"moment-timezone": "^0.5.0",
6162
"phantomjs-prebuilt": "^2.1.4",
6263
"plato": "^1.5.0",
6364
"run-browser": "^2.0.2",

paths.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var modules = [
44
'node_modules/jquery/dist/jquery.js',
55
'node_modules/moment/moment.js',
66
'node_modules/moment/locale/*.js',
7+
'node_modules/moment-timezone/builds/moment-timezone-with-data.js',
78
'node_modules/bootstrap/dist/js/bootstrap.js',
89
'node_modules/angular/angular.js',
910
'node_modules/angular-mocks/angular-mocks.js'

src/js/datetimepicker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,13 @@
315315
// No additional work needed
316316
break;
317317
case 'moment':
318-
newDate = moment(newDate);
318+
newDate = moment([tempDate.getUTCFullYear(), tempDate.getUTCMonth(), tempDate.getUTCDate(), tempDate.getUTCHours(), tempDate.getUTCMinutes(), tempDate.getUTCSeconds(), tempDate.getUTCMilliseconds()]);
319319
break;
320320
case 'milliseconds':
321321
newDate = milliseconds;
322322
break;
323323
default: // It is assumed that the modelType is a formatting string.
324-
newDate = moment(newDate).format(configuration.modelType);
324+
newDate = moment([tempDate.getUTCFullYear(), tempDate.getUTCMonth(), tempDate.getUTCDate(), tempDate.getUTCHours(), tempDate.getUTCMinutes(), tempDate.getUTCSeconds(), tempDate.getUTCMilliseconds()]).format(configuration.modelType);
325325
}
326326

327327
var oldDate = ngModelController.$modelValue;

test/model/modelType.spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('modelType', function () {
1818
$compile = _$compile_;
1919
$rootScope = _$rootScope_;
2020
$rootScope.date = null;
21+
moment.tz.setDefault(null);
2122
}));
2223

2324
describe('throws exception', function () {
@@ -142,6 +143,32 @@ describe('modelType', function () {
142143

143144
expect(moment('1999-01-01').isSame($rootScope.date)).toBeTruthy();
144145
});
146+
it('returns a moment with correct time zone', function () {
147+
148+
$rootScope.date = 1132185600000; // '2005-11-17'
149+
moment.tz.setDefault('America/Los_Angeles');
150+
151+
var element = $compile('<datetimepicker data-ng-model="date" data-datetimepicker-config="{ modelType: \'moment\', startView: \'year\', minView: \'year\' }"></datetimepicker>')($rootScope);
152+
$rootScope.$digest();
153+
154+
var selectedElement = jQuery(jQuery('.year', element)[0]);
155+
selectedElement.trigger('click');
156+
157+
expect(moment('1999-01-01T00:00:00-08:00').isSame($rootScope.date)).toBeTruthy();
158+
});
159+
it('returns a moment with correct time zone', function () {
160+
161+
$rootScope.date = 1132185600000; // '2005-11-17'
162+
moment.tz.setDefault('America/New_York');
163+
164+
var element = $compile('<datetimepicker data-ng-model="date" data-datetimepicker-config="{ modelType: \'moment\', startView: \'year\', minView: \'year\' }"></datetimepicker>')($rootScope);
165+
$rootScope.$digest();
166+
167+
var selectedElement = jQuery(jQuery('.year', element)[0]);
168+
selectedElement.trigger('click');
169+
170+
expect(moment('1999-01-01T00:00:00-05:00').isSame($rootScope.date)).toBeTruthy();
171+
});
145172
it('throws an exception if invalid date string is in the model', function () {
146173

147174
$rootScope.date = 'invalid-date';
@@ -263,6 +290,32 @@ describe('modelType', function () {
263290

264291
expect($rootScope.date).toBe('gibb5ri012');
265292
});
293+
it('returns formatted string in correct time zone', function () {
294+
295+
$rootScope.date = moment('2005-11-17').toDate();
296+
moment.tz.setDefault('America/Los_Angeles');
297+
298+
var element = $compile('<datetimepicker data-ng-model="date" data-datetimepicker-config="{ modelType: \'MM-DD-YYYY Z\', startView: \'year\', minView: \'year\' }"></datetimepicker>')($rootScope);
299+
$rootScope.$digest();
300+
301+
var selectedElement = jQuery(jQuery('.year', element)[0]);
302+
selectedElement.trigger('click');
303+
304+
expect($rootScope.date).toBe('01-01-1999 -08:00');
305+
});
306+
it('returns formatted string in correct time zone', function () {
307+
308+
$rootScope.date = moment('2005-11-17').toDate();
309+
moment.tz.setDefault('America/New_York');
310+
311+
var element = $compile('<datetimepicker data-ng-model="date" data-datetimepicker-config="{ modelType: \'MM-DD-YYYY Z\', startView: \'year\', minView: \'year\' }"></datetimepicker>')($rootScope);
312+
$rootScope.$digest();
313+
314+
var selectedElement = jQuery(jQuery('.year', element)[0]);
315+
selectedElement.trigger('click');
316+
317+
expect($rootScope.date).toBe('01-01-1999 -05:00');
318+
});
266319
it('throws an exception if numeric string is in the model', function () {
267320

268321
$rootScope.date = '1132185600000';

0 commit comments

Comments
 (0)