Jump to content

date problem...


Recommended Posts

hello there, I have some sort of problem with date in javascript... lemme just post my code here...

var currDate = new Date();var currDateEnd = new Date(); currDateEnd = currDate;currDateEnd.setDate(currDate.getDate()+7); alert(currDate); //I tried alert currDate here, and looks like this object (currDate) value has been changed to the value of currDateEnd

I know there's other way to surpass this problem, but I want to know first why this is happening... Thanks a lot, very much...

Edited by newrehmi
Link to comment
Share on other sites

Objects are always referenced and not copied, so both currDate and currDateEnd are pointing to the same object because of this line:

currDateEnd = currDate; 

You can remove that line. If you want to copy the value of a date object to a new one, do this:

var currDate = new Date();var currDateEnd = new Date(currDate.getTime());

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...