Javascript Uncaught TypeError : .split 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52547876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:58:25  来源:igfitidea点击:

Javascript Uncaught TypeError : .split is not a function

javascript

提问by Ivana Mica

I wanted to make a function where user can only claim coins once per day. I did the function .splitso that it compares the date only since Date()only compares both date and time. However, i got this javascript error:

我想做一个功能,用户每天只能领取一次硬币。我做了这个函数,.split以便它只比较日期,因为Date()只比较日期和时间。但是,我收到了这个 javascript 错误:

Uncaught TypeError (intermediate value).split is not a function

未捕获的 TypeError(中间值)。split 不是函数

Anyone knows on how to solve this problem? I've tried so many ways. The error is still there.

有谁知道如何解决这个问题?我尝试了很多方法。错误仍然存​​在。

Here's my code:

这是我的代码:

$(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;
  }   
})

回答by DILEEP THOMAS

ISSUE

问题

var date = new Date();

var claimedDate = new Date(date.setDate(date.getDate()-1)) ;
var todaysDate = new Date()


// converting toString and splitting up

claimedDate = claimedDate.toDateString().split(" ");

todaysDate = new Date().toDateString().split(" ");

// result date with array of Day, MonthName, Date and Year

console.log("claimed date", claimedDate)
console.log("todays date", todaysDate)

`var d = new Date();` // Todays date

if you do a d.split(" "):: gives you an error d.split is not a function

如果你做一个d.split(" "):: 给你一个错误 d.split is not a function

you can split it by d.toDateString().split(" ")// gives you an array of ["Fri", "Sep", "28", "2018"]`

你可以通过d.toDateString().split(" ")// 给你一个数组["Fri", "Sep", "28", "2018"]`

using the above you can check with the previous date

使用上面的你可以检查上一个日期

you can check the toDateString method, now the array consist of Day, month, date, and year. So you can check the previous date and you can disable or enable the button.

您可以检查toDateString 方法,现在数组由日、月、日和年组成。因此,您可以检查上一个日期,并且可以禁用或启用该按钮。

BETTER SOLUTION

更好的解决方案

No need to convert it toString and split , you can direclty check the two dates directly, check the solution

不需要转换为String和split,可以直接直接查看两个日期,查看解决方法

SOLUTION

解决方案

$(document).ready(function () {
  var date = new Date();

  var lastClaimedDate = new Date(date.setDate(date.getDate() -  1 )); 
  
  var currentDate = new Date();
  

  if(lastClaimedDate < currentDate){
    $("#btnAddCoins").prop("disabled", true)
  }else{
    $("#btnAddCoins").prop("disabled", false)
  }
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnAddCoins">Add Coins</button>

回答by Mike

You can coerce your date into a string and then split on it:

您可以将日期强制转换为字符串,然后对其进行拆分:

let strDate = (''+new Date()).split(' ')[0]

console.log( strDate ) 

This is the wrong solution for your problem, though. Consider comparing the date object and not the strings.

但是,这是您问题的错误解决方案。考虑比较日期对象而不是字符串。

let strLastClaimedDate = '01/02/2017 01:30:00'
let dtLastClaimedDate = new Date(strLastClaimedDate)
console.log(formatDate(dtLastClaimedDate))

if ( formatDate(dtLastClaimedDate) < formatDate(new Date('01/02/2017 02:00:00')) )
  console.log('date is older')
else
  console.log('same day (or after)')


function formatDate(dt){
  let month = dt.getMonth()+1
  month = month < 10 ? '0'+month : month
  
  let day = dt.getDate()
  day = day < 10 ? '0'+day : day

  return [dt.getFullYear(),month,day].join('')
}