4

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

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

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;
  }   
})
4
  • The split() method splits a String object into an array of strings by separating the string into substrings not on Date object Commented Sep 28, 2018 at 3:33
  • use (new Date).getDate() instead of split Commented Sep 28, 2018 at 3:35
  • like this (new Date(model[0].lastClaimedDate).getDate() < new Date().getDate() ?? Commented Sep 28, 2018 at 3:38
  • change date to string using toDateString() method, so your code will become new Date(model[0].lastClaimedDate).toDateString().split(' ')[2] > new Date().toDateString().split(' ')[2] Commented Sep 28, 2018 at 5:41

3 Answers 3

8

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

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>

Sign up to request clarification or add additional context in comments.

8 Comments

var d = new Date(); d.toString().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; } }) in this way?
you can store the variables of lastClaimedDate and todays date, by converting them to string and split, so each variable will have an array consist of Day, MonthName, Date, and Year. For Example ["Thu", "Sep", "27", "2018"] and ["Fri", "Sep", "28", "2018"] From this two array you have the required information so you can check that. I think this is not a good solution , i will check for a better method, but for your question to split the date. First you need to convert it to String.
if you manage to find out, can you post the code here. Thank you so much
@IvanaMica toString needs to be updated wth toDateString , i was using toDateString but i have written on the solution is toString. Updated the solution with sample code. Please let me know whether you got the solution
so i replaced the console.log with the button disabled and enabled button code?
|
0

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('')
}

1 Comment

@IvanaMica the if statement hasn't really changed, the console demonstrates when they're triggered so that you can play with the value 01/02/2017 to see if this is what you were expecting
0

Instead of using split, you can try to use the spread operator. e.g. arr.split('') could be [...arr]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.