1 Reply Latest reply on Apr 13, 2010 12:43 AM by Sebastien V.

    Date Confusion

    jfillman Level 1

      I have two date variables defined like this:

       

      private var baseDate:Date = new Date();  //April 12, 2010

      private var currentDate:Date = new Date();  //April 12, 2010

       

      When the application initialized, I do the following to the dates:

       

      currentDate = baseDate;

       

       

      currentDate.date = (baseDate.date - baseDate.day);

       

      What is confusing to me is that this ends of setting baseDate AND the currentDate to April 11, 2010, where I would only expect the currentDate to be April 11, 2010.

       

      I know that I can do this:

       

      currentDate = newDate(baseDate.fullYear, baseDate.month, baseDate.date - baseDay.day)

       

      but there are other areas where I need to set parts of dates based on other dates, and would rather not create a new Date object every time. I could end up with hundreds or thousands over the course of a users interaction, and it's not clear to me whether or not they get cleaned up by the garbage collection process, so I guess that's part 2 of the question, I generally try to avoid new... inside of a function, as these things get created and never go away.

        • 1. Re: Date Confusion
          Sebastien V. Level 3

          The only think to keep in mind here is that when you use

          currentDate = baseDate

          , it is a reference assignation. In other works, the Date that used to be referenced by currentDate is lost (unless it is being referenced by another var), and that after this line of code the two var currentDate and baseDate are refering to the exact same object.

          That is why once you've done this, any chage you make on currentDate will be made on baseDate. So you must use the new Date() constructor every time you wish to keep two separates Date instances.

           

          You can also use this to "make copy" of dates :

           

          currentDate = new Date( baseDate );