Skip to content

Commit 6bea33c

Browse files
committed
Add returnType and returnFormat parameters
Allow output to be parsed and formatted with momentjs using returnFormat pattern.
1 parent a0c90a4 commit 6bea33c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/js/datetimepicker.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
dropdownSelector: null,
2929
minuteStep: 5,
3030
minView: 'minute',
31-
startView: 'day'
31+
startView: 'day',
32+
returnType: 'date',
33+
returnFormat: 'YYYY-MM-DD'
3234
})
3335
.directive('datetimepicker', ['$log', 'dateTimePickerConfig', function datetimepickerDirective($log, defaultConfig) {
3436

@@ -50,7 +52,7 @@
5052
}
5153

5254
var validateConfiguration = function validateConfiguration(configuration) {
53-
var validOptions = ['startView', 'minView', 'minuteStep', 'dropdownSelector'];
55+
var validOptions = ['startView', 'minView', 'minuteStep', 'dropdownSelector', 'returnType', 'returnFormat'];
5456

5557
for (var prop in configuration) {
5658
//noinspection JSUnfilteredForInLoop
@@ -61,6 +63,7 @@
6163

6264
// Order of the elements in the validViews array is significant.
6365
var validViews = ['minute', 'hour', 'day', 'month', 'year'];
66+
var validReturnTypes = ['moment', 'date'];
6467

6568
if (validViews.indexOf(configuration.startView) < 0) {
6669
throw ('invalid startView value: ' + configuration.startView);
@@ -83,6 +86,10 @@
8386
if (configuration.dropdownSelector !== null && !angular.isString(configuration.dropdownSelector)) {
8487
throw ('dropdownSelector must be a string');
8588
}
89+
90+
if (validReturnTypes.indexOf(configuration.returnType) < 0) {
91+
throw ("returnType must be either moment or date. Received: " + configuration.returnType);
92+
}
8693

8794
/* istanbul ignore next */
8895
if (configuration.dropdownSelector !== null && ((typeof jQuery === 'undefined') || (typeof jQuery().dropdown !== 'function'))) {
@@ -336,7 +343,9 @@
336343

337344
setTime: function setTime(unixDate) {
338345
var tempDate = new Date(unixDate);
339-
var newDate = new Date(tempDate.getTime() + (tempDate.getTimezoneOffset() * 60000));
346+
var newDate = configuration.returnType === 'date'
347+
? new Date(tempDate.getTime() + (tempDate.getTimezoneOffset() * 60000))
348+
: moment(newDate).format(configuration.returnFormat);
340349

341350
var oldDate = ngModelController.$modelValue;
342351
ngModelController.$setViewValue(newDate);

0 commit comments

Comments
 (0)