【发布时间】:2018-09-28 03:31:07
【问题描述】:
我想制作一个用户每天只能领取一次硬币的功能。
我做了函数.split,所以它只比较日期,因为Date()只比较日期和时间。但是,我收到了这个 javascript 错误:
Uncaught TypeError (intermediate value).split 不是函数
有人知道如何解决这个问题吗?我已经尝试了很多方法。错误仍然存在。
这是我的代码:
$(document).ready(function () {
if (new Date(model[0].lastClaimedDate).split(' ')[0] < new Date().split(' ')[0]) {
document.getElementById('btnAddCoins').disabled = false;
}
else {
document.getElementById('btnAddCoins').disabled = true;
}
})
【问题讨论】:
-
split() 方法将字符串对象拆分为字符串数组,方法是将字符串拆分为不在 Date 对象上的子字符串
-
使用
(new Date).getDate()而不是拆分 -
像这样 (new Date(model[0].lastClaimedDate).getDate()
-
使用 toDateString() 方法将日期更改为字符串,因此您的代码将变为
new Date(model[0].lastClaimedDate).toDateString().split(' ')[2] > new Date().toDateString().split(' ')[2]
标签: javascript