Jump to content

date problem...


newrehmi

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...

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());

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...