AngularJS:RangeError:Date.toISOString(<anonymous>)上的无效时间值

Jak*_*kub 3 javascript jquery date datepicker angularjs

在我的项目中,我必须在jquery(angular-jquery-datepicker)日期选择器中显示日期,该日期的格式应适合用户所在区域的正确格式。我能够以美国和欧盟格式显示。当用户设置这些日期时,我必须将其与toISOString一起保存到数据库中。但是,对于美国来说,一点问题都没有问题。对于EU格式,我收到标题中张贴的错误,并且我要分享整个错误:

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)
Run Code Online (Sandbox Code Playgroud)

我不知道为什么在美国使用日期有效而在欧盟无效。我不明白为什么某个日期有效,而另一日期却无效。

下面,我分享了给出该错误的代码:

 $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
 }
Run Code Online (Sandbox Code Playgroud)

我在代码$ 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();
            }
        });
    }
};
});
Run Code Online (Sandbox Code Playgroud)

Cod*_*ody 5

事实证明,这是在调用时发生toISOStringInvalid Date。之所以会发生这种情况,是因为您输入的new Date()结构是undefined- - 可能是Invalid Date因为您正在将UTC偏移量处理为

这是一个简短的代码段,用于在尽可能少的行中传达确切的错误:

clear();
;(function iif() {
    var d = new Date(undefined);  // .valueOf() === 'Invalid Date'
    console.log('>', d, +d);
    if (isNaN(+d)) return;
    d.toISOString();
    console.log('- executed -');
})();
Run Code Online (Sandbox Code Playgroud)

将其扔到JS控制台中并进行处理-每当在上调用时toISOString,都会重现该错误new Date(undefined)if (isNaN(+d)) return;表示“如果UTC时间戳记不是数字,则只是中止任务” – +new Date()与相同(new Date()).valueOf()

对您来说,杠杆作用的一个要点是:确保您的虚假日期为null0-不为undefined""(空字符串)。

希望这可以帮助!