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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...