【问题标题】:AngularJS: RangeError: Invalid time value at Date.toISOString (<anonymous>)AngularJS: RangeError: Date.toISOString (<anonymous>) 的时间值无效
【发布时间】: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


    【解决方案1】:

    事实证明,在 Invalid Date 上调用 toISOString 时会发生这种情况。这可能是因为您对 new Date() 构造的输入是 undefined -- OR -- 因为 也许您正在将 UTC-Offset 操作为 @ 987654325@.

    这里有一个小sn-p,可以用尽可能少的行来传达确切的错误:

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

    将它扔到您的 JS 控制台中并使用它——您将在任何时候在 new Date(undefined) 上调用 toISOString 时重现该错误。 if (isNaN(+d)) return; 表示“如果 UTC 时间戳不是数字,则中止任务” -- +new Date()(new Date()).valueOf() 相同。

    您的一个杠杆点:确保您的错误日期是null0——不是undefined""(空字符串)。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 2019-12-23
      • 2021-08-30
      • 1970-01-01
      • 2020-12-31
      • 2021-08-22
      • 2020-10-31
      • 2021-06-16
      相关资源
      最近更新 更多