0

Why am I not able to increment the day by clicking on +?

  var dayLength = 1000*3600*24;
  var current = new Date();
  console.log(current);
  $('button').on('click', function(){
    var a = new Date((new Date()).valueOf() + dayLength);
    console.log(a);
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-default">+</button>

1 Answer 1

1

You add dayLength to new Date() which is current date. You should add it to current variable.

  var dayLength = 1000*3600*24;
  var current = new Date();
  console.log(current);

  $('button').on('click', function(){
    current = new Date(current.getTime() + dayLength);
    console.log(current);
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-default">+</button>

Note that I also change valueOf to getTime because per docs, valueOf is usually not called in user code.

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

1 Comment

Thanks Niyoko, this is working but can you please let me know what is the point of using window.current? why not var current?

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.