1

I have a form, where I need to do some validations by getting the current date and next date separately using javaScript.

I tried this code but it does not work correctly.

var currentDate = new Date();//get current date

var validdate=currentDate;
validdate.setDate(validdate.getDate()+1);

But what happens is when valid date changes to +1, then current date also changes to +1.

But I need it separately, means when I print the output: I get

currentDate = 5th May 2014;

validdate = 6th May 2014;

Then when again I print currentDate, it changes to "6th May 2014".

How to get the current date and next date without affecting each other?

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
  • I dont know why is this happening , but it is interesting, it will be nice if someone could explain how the current date is getting affeted – Amarnath R Shenoy May 05 '14 at 07:26

4 Answers4

3

instead of validdate = currentDate; try validdate = new Date();.

Your problem happens because in javascript when you pass an object to a value it is passed by reference and not by value. So when you change it you change even the original object because you don't actually have two different objects.

Mir
  • 1,575
  • 1
  • 18
  • 31
  • can u please explain why this happens more clearly.cant understand – user3596069 May 05 '14 at 07:26
  • @user3596069 that is because when you do validate = currentDate, validate basically becomes another name for currentDate, so changing validate will change currentDate – kennypu May 05 '14 at 07:30
  • @user3596069 there is a good explanation on this subject here http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value – Mir May 05 '14 at 07:31
2

you are refering to same object instance for currentDate and validdate.

initialise them seperatly like below

currentDate = new Date();

validdate = new Date();

also rename your validdate to validDate for more readability.

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
  • but i am only tring to change validdate by this code not the current one still it affects it validdate.setDate(validdate.getDate()+1); – user3596069 May 05 '14 at 07:28
  • no, if you use his method it will work just fine. The only problem is that the dates may be a little off – kennypu May 05 '14 at 07:29
1

to make a new copy of a Date object use following code

var validdate = new Date(currentDate.getTime());

now when you modify the validdate object it will not update currentDate

user2321864
  • 2,207
  • 5
  • 25
  • 35
  • can u explain why this issue happens when i only modify validdate not the currentdate – user3596069 May 05 '14 at 07:29
  • when you assign validdate = currentDate; you are referencing to same object instance for currentDate, and afterwards when you modify validdate you are modifying the same object being referenced by both validdate and currentDate. if you copy the instance then validdate and currentDate reference totally different instances and modifications to validdate does not update currentDate – user2321864 May 05 '14 at 07:36
0

Try This

var currentDate = new Date();//get current date
alert(currentDate.toString());
var validdate=new Date(); 
validdate.setDate(currentDate.getDate()+1);
alert(validdate.toString());
alert(currentDate.toString());
Amarnath R Shenoy
  • 5,121
  • 8
  • 24
  • 32