【发布时间】:2017-10-31 23:09:27
【问题描述】:
在我的项目中,我必须在 jquery (angular-jquery-datepicker) 日期选择器中显示日期,该日期以正确的方式为用户在他/她的区域中格式化。 我能够以美国和欧盟格式展示。当用户设置这些日期时,我必须将其与 toISOString 一起保存到数据库中。但是,对于美国来说,没有任何问题可以正常工作。对于欧盟格式,我收到了标题中发布的错误,我正在分享整个错误:
RangeError: Invalid time value
at Date.toISOString (<anonymous>)
at n.$scope.save (scripts.js:2826)
at angular.js:12474
at f (angular.js:21700)
at n.$eval (angular.js:14570)
at n.$apply (angular.js:14669)
at HTMLButtonElement.<anonymous> (angular.js:21705)
at HTMLButtonElement.dispatch (jquery.min.js:3)
at HTMLButtonElement.r.handle (jquery.min.js:3)
我不知道为什么美国日期有效而欧盟日期无效。我不明白为什么一个日期有效,而另一个日期无效。
我在下面分享给出该错误的一段代码:
$scope.save = function() {
if ($scope.banner.fromText != null && $scope.banner.toText != null) {
$scope.banner.from = new Date($scope.banner.fromText);
$scope.banner.to = new Date($scope.banner.toText);
$scope.banner.to.setHours(23, 59, 59)
$scope.banner.from = $scope.banner.from.toISOString()
$scope.banner.to = $scope.banner.to.toISOString()
}else{
$scope.banner.to = null
$scope.banner.from = null
}
我在代码中使用 $filter('date')(value.item.from, 'shortDate');
但是,如果您需要查看其他内容以了解此错误,请告诉我我应该发布什么以便更好地查看它
我的日期选择器指令:
'use strict';
angular.module('angular-jquery-datepicker', []).factory('datepicker',
[function() {
return $.datepicker;
}]).provider('$datepicker', function() {
return {
setDefaults: function(language) {
$.datepicker.setDefaults($.datepicker.regional[language]);
},
$get: function() {
return {
}
}
}
}).directive('jqdatepicker', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
element.datepicker({
//dateFormat: 'dd-mm-yy',
autoclose: true,
changeMonth: true,
changeYear: true,
maxDate: Date.today(),
showOn: "both",
buttonImage: "/img/calendar-o.png",
onSelect: function(date) {
ctrl.$setViewValue(date);
ctrl.$render();
scope.$apply();
}
});
}
};
/*Datepicker for banner - extended main datepicker*/
}).directive('jqdatepickerbanner', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
//scope.customStartDate = today;
element.datepicker({
//dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
//maxDate: '+6mm', // 6 Months max date --> For set the maximum period the Banner can be set up
minDate: Date.today(),
showOn: "both",
buttonImage: "/img/calendar-o.png",
onSelect: function(date) {
if (this.id === "startDateBanner") {
$("#endDateBanner").datepicker("option", "minDate", date);
}
ctrl.$setViewValue(date);
ctrl.$render();
scope.$apply();
}
});
}
};
});
【问题讨论】:
标签: javascript jquery angularjs date datepicker