0

I'm new to Javascript. The code below compare both date and time. I wanted to make a function where user can only claim the coins once for each day. However, when user claims the coins on the next day, the button is still disabled because the code below follow the time that user last claimed their coins. I've tried so many ways but it still comparing both date and time. Anyone knows on how to compare the date without time?

        $(document).ready(function () {
                if (new Date(model[0].lastClaimedDate) < new Date()) {
                    document.getElementById('btnAddCoins').disabled = false;
                }
                else {
                    document.getElementById('btnAddCoins').disabled = true;
                }   
        })
Rod
  • 61
  • 1
  • 10

2 Answers2

2
new Date(model[0].lastClaimedDate).setHours(0,0,0,0) < new Date()

This will set the time values to 0 in the datetime object being saved for the user

CopyPasterino
  • 170
  • 11
  • there's an error : (intermediate value).split is not a function – Rod Sep 28 '18 at 01:59
  • the error is not there but **the button is disabled**. It should be enabled. I just want **only the date**. Since I claimed yesterday, I can claim today. – Rod Sep 28 '18 at 04:01
  • Can you print out both of the values and tell me what they are for debugging purposes? Maybe something unexpected is happening – CopyPasterino Sep 28 '18 at 04:05
  • `$("#CoinsBtn").click(function () { $('#total').text(parseInt($('#total').text()) + parseInt($(this).data('amount'))); var button = $(this); button.attr('disabled', 'disabled'); setTimeout(function () { button.removeAttr('disabled'); }, 86400000); });` is it because i added this code? – Rod Sep 28 '18 at 04:24
  • yes you are disabling with button.attr('disabled', 'disabled'); so the button is disabled on page load – CopyPasterino Sep 28 '18 at 04:28
  • only keep the part before var button is declared – CopyPasterino Sep 28 '18 at 04:32
  • so i remove the part of code from **button.attr to 86400000** ?? – Rod Sep 28 '18 at 04:34
  • that will work but i meant from var to 86400000 why declare the variable if you arent using it – CopyPasterino Sep 28 '18 at 04:55
0

Did you try this:

new Date(model[0].lastClaimedDate).getDate() < new Date().getDate()
kntrieu
  • 64
  • 3