将日期与NaN和未定义进行比较(Comparing date to NaN and undefined)

编程入门 行业动态 更新时间:2024-10-28 18:27:49
日期与NaN和未定义进行比较(Comparing date to NaN and undefined)

我有一个非常简单的if语句,但我不太使用javascript所以我认为我在某个地方犯了错误。 如果你转到我的页面,你可以看到值被提醒为未定义,但即使if参数== undefined仍然会跳过一段代码。 这是一个AngularJS应用程序是否重要?

网页: http : //alainwebdesign.ca/pl2/#/petType

JavaScript的:

$scope.setDate = function (dateSelected) { alert(dateSelected); var now = new Date(); $scope.latest = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes()); $scope.hoursAgo = Math.round((($scope.latest - dateSelected) / 3600000) * 100) / 100; if ($scope.hoursAgo === undefined || $scope.hoursAgo == NaN) { alert("Please choose a date/time"); } else { alert('You lost your pet ' + $scope.hoursAgo + ' hour(s) ago'); $scope.checkDateSet = true; } }

I have a pretty simple if statement but I don't use javascript too much so I think I have made an error somewhere. If you go to my page you can see the value gets alerted as undefined, but a block of code still gets skipped even though the if parameters are == undefined. Does it matter that this is an AngularJS app?

web page: http://alainwebdesign.ca/pl2/#/petType

javascript:

$scope.setDate = function (dateSelected) { alert(dateSelected); var now = new Date(); $scope.latest = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes()); $scope.hoursAgo = Math.round((($scope.latest - dateSelected) / 3600000) * 100) / 100; if ($scope.hoursAgo === undefined || $scope.hoursAgo == NaN) { alert("Please choose a date/time"); } else { alert('You lost your pet ' + $scope.hoursAgo + ' hour(s) ago'); $scope.checkDateSet = true; } }

最满意答案

检查if ($scope.hoursAgo === undefined || $scope.hoursAgo == NaN)

像这样写

if ($scope.hoursAgo === 'undefined' || isNaN($scope.hoursAgo)) {

To check if ($scope.hoursAgo === undefined || $scope.hoursAgo == NaN)

write like this

if ($scope.hoursAgo === 'undefined' || isNaN($scope.hoursAgo)) {

更多推荐

本文发布于:2023-07-23 04:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1227549.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   未定义   NaN   undefined   Comparing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!