gpt4 book ai didi

JavaScript 类型转换不是数字

转载 作者:行者123 更新时间:2023-12-01 05:31:06 25 4
gpt4 key购买 nike

我有一个 div,其数据属性包含日期。当我检索所述数据值时,它不是日期,而是字符串:

<div id="firstRow" class="form-group" data-expense-date="5/3/2016">
<span>some stuff in here</span>
</div>

我正在检索数据元素,如下所示:

var currentDateString = $(this).data('expense-date');

然后我尝试将值拆分为字符串数组:

var currentDateArray = currentDateString.split("/");

然后,我将数组的一部分分配为我尝试创建的日期的变量:

var year = Number(currentDateArray[2].toString());
var month = Number(currentDateArray[0].toString());
var day = Number(currentDateArray[1].toString());

有趣的是,我的所有三个变量都说它们是 NaN。因此,我的新日期变量的声明是“无效日期”:

var currentDate = new Date(year, month - 1, day);

我可以执行以下操作,并且效果很好:

var dateString = "5/3/2016";
var dateStringArray = dateString.split("/");
var testDate = new Date(dateStringArray[2], dateStringArray[0] - 1, dateStringArray[1]);

只有当我从 div 元素读取 data 属性时,才会将值设置为我不知道它是什么的值。

我正在 Visual Studio 中进行调试,这是我尝试测试值的立即窗口的屏幕截图:

enter image description here

这是完整的示例...我的 HTML:

<div class="col-md-6" id="detailPreview">
<div class="panel panel-transparent" data-expense-date="5/2/2016">
<div class="panel-heading">
<h3 class="panel-title">Monday, May 2, 2016</h3>
<div class="panel-actions">
<a class="panel-action icon wb-minus" data-toggle="panel-collapse" aria-expanded="true" aria-hidden="true"></a>
</div>
</div>
<div class="panel-body expense-date-body">
<div class="panel" data-expense-detail-id="5e18bb2b-4671-490d-8769-5d3ea08134d8">
<div class="panel-heading">
<h3 class="panel-title"><i class="icon wb-payment" aria-hidden="true"></i>Expense: Best Buy (ASUS Z555RA-3)</h3>
<div class="panel-actions">
<a class="panel-action panel-action-edit icon wb-settings" aria-hidden="true"></a>
<a class="panel-action icon wb-minus" data-toggle="panel-collapse" aria-expanded="false" aria-hidden="true"></a>
<a class="panel-action icon wb-close" data-toggle="panel-close" aria-hidden="true"></a>
</div>
</div>
<!-- <div class="panel-collapse"> -->
<div class="panel-body">
Business Name: Best Buy
Location: Topeka, KS
Purpose: Purchased replacement laptop for inventory
</div>
<!-- </div> -->
</div>
</div>
</div>
<div class="panel panel-transparent" data-expense-date="5/3/2016">
<div class="panel-heading">
<h3 class="panel-title">Tuesday, May 3, 2016</h3>
<div class="panel-actions">
<a class="panel-action icon wb-minus" data-toggle="panel-collapse" aria-expanded="true" aria-hidden="true"></a>
</div>
</div>
<div class="panel-body expense-date-body">
<div class="panel" data-expense-detail-id="49de85e2-b3ef-465d-8a61-cbc298b99d34">
<div class="panel-heading">
<h3 class="panel-title"><i class="icon wb-payment" aria-hidden="true"></i>Expense: Wal-Mart (Scotch Tape)</h3>
<div class="panel-actions">
<a class="panel-action panel-action-edit icon wb-settings" aria-hidden="true"></a>
<a class="panel-action icon wb-minus" data-toggle="panel-collapse" aria-expanded="false" aria-hidden="true"></a>
<a class="panel-action icon wb-close" data-toggle="panel-close" aria-hidden="true"></a>
</div>
</div>
<!-- <div class="panel-collapse"> -->
<div class="panel-body">
Business Name: Wal-Mart
Location: Topeka, KS
Purpose: Purchased scotch tape
</div>
<!-- </div> -->
</div>
</div>
</div>
</div>

我的 JavaScript:

$("[data-expense-date]").each(function () {
if ($(this).data('expense-date') === dateShort.toString()) {
parentDiv = $(this);
return false;
} else {
debugger;

// determine if this date is before or after the param date.
//var dataExpenseDate = $(this).data('expense-date');
var currentDateString = $(this).data('expense-date'); // "5/2/2016"

var currentDateArray = currentDateString.split('/'); // [5,2,2016]
//var yearString = currentDateArray[2];
//var yearNumber = Number(yearString);
var year = Number(currentDateArray[2]); // "2016" <-- NaN
var month = Number(currentDateArray[0]); // "5" <-- NaN
var day = Number(currentDateArray[1]); // "3" <-- NaN

var currentDate = new Date(year, month - 1, day); // Invalid Date

if (date > currentDate) {
newDivInjectAfter = $(this);
}
}
});

更新:
仅当我使用 Internet Explorer (11) 时才会发生这种情况。 Edge、Opera、FireFox 和 Chrome 都可以正确解析值。感谢大家的回复,但我相信我发现了 IE 的一个错误。

最佳答案

搜索我的问题并将 Internet Explorer 添加到搜索后,我在这里得到了结果。它似乎已使用正则表达式解决:

Strange behaviour with spliting a string in Javascript

关于JavaScript 类型转换不是数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37281614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com