GUAC-1213: Move date/time parsing code to own functions.

This commit is contained in:
Michael Jumper
2015-08-10 11:30:06 -07:00
parent 4db7034608
commit b2305937c5
2 changed files with 36 additions and 2 deletions

View File

@@ -48,9 +48,26 @@ angular.module('form').controller('dateFieldController', ['$scope', '$injector',
}; };
/**
* Parses the date components of the given string into a Date with only the
* date components set. The resulting Date will be in the UTC timezone,
* with the time left as midnight. The input string must be in the format
* YYYY-MM-DD (zero-padded).
*
* @param {String} str
* The date string to parse.
*
* @returns {Date}
* A Date object, in the UTC timezone, with only the date components
* set.
*/
var parseDate = function parseDate(str) {
return new Date(str + 'T00:00Z');
};
// Update typed value when model is changed // Update typed value when model is changed
$scope.$watch('model', function modelChanged(model) { $scope.$watch('model', function modelChanged(model) {
$scope.typedValue = (model ? new Date(model + 'T00:00Z') : null); $scope.typedValue = (model ? parseDate(model) : null);
}); });
// Update string value in model when typed value is changed // Update string value in model when typed value is changed

View File

@@ -48,9 +48,26 @@ angular.module('form').controller('timeFieldController', ['$scope', '$injector',
}; };
/**
* Parses the time components of the given string into a Date with only the
* time components set. The resulting Date will be in the UTC timezone,
* with the date left as 1970-01-01. The input string must be in the format
* HH:MM:SS (zero-padded, 24-hour).
*
* @param {String} str
* The time string to parse.
*
* @returns {Date}
* A Date object, in the UTC timezone, with only the time components
* set.
*/
var parseTime = function parseTime(str) {
return new Date('1970-01-01T' + str + 'Z');
};
// Update typed value when model is changed // Update typed value when model is changed
$scope.$watch('model', function modelChanged(model) { $scope.$watch('model', function modelChanged(model) {
$scope.typedValue = (model ? new Date('1970-01-01T' + model + 'Z') : null); $scope.typedValue = (model ? parseTime(model) : null);
}); });
// Update string value in model when typed value is changed // Update string value in model when typed value is changed