Javascript Uncaught TypeError : .split is not a function

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)

 Run code snippetExpand snippet

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

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

you can split it by d.toDateString().split(" ") // gives you an array of [“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.

BETTER SOLUTION

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

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>

Leave a Comment