13 Replies Latest reply on Nov 26, 2007 12:45 AM by Greg Dove

    Looks like a bug in Date class

    naleen80
      Hi guys,
      I just logged in to highlight what I encountered when I was trying to convert milliseconds of a time stamp to a date/time string.
      Just see the following codes.

      public function convertTimeStampToDateAndTime(timeStampInMilliseconds:Number):String
      {
      var stampReadableFormate:String = null;

      if(!isNaN(timeStampInMilliseconds))
      {

      var stampDate:Date = new Date(timeStampInMilliseconds);

      var stampHour:Number = stampDate.getHours();
      var stampMin:Number = stampDate.getMinutes();
      var stampSec:Number = stampDate.getSeconds();

      var stampYear:Number = stampDate.getFullYear();
      var stampMonth:Number = stampDate.getMonth() + 1;
      var stampDate:Number = stampDate.getDate();

      stampReadableFormate = "" + stampDate + "/" + stampMonth + "/" + stampYear + " " + stampHour + ":" + stampMin + ":" + stampSec;

      trace("Date/Time=" + stampReadableFormate);
      }

      return stampReadableFormate;
      }

      Then I call this method from somewhere by using,
      var reportTime:String = this.convertTimeStampToDateAndTime(new Date().getTime());


      You can see trace method in convertTimeStampToDateAndTime() is giving you a correct output.

      But just change the convertTimeStampToDateAndTime() method in a way that you get time information after you got date, month and Year. To explain what I am taking about I have pasted changed code as following.


      public function convertTimeStampToDateAndTime(timeStampInMilliseconds:Number):String
      {
      var stampReadableFormate:String = null;

      if(!isNaN(timeStampInMilliseconds))
      {

      var stampDate:Date = new Date(timeStampInMilliseconds);

      var stampYear:Number = stampDate.getFullYear();
      var stampMonth:Number = stampDate.getMonth() + 1;
      var stampDate:Number = stampDate.getDate();

      var stampHour:Number = stampDate.getHours();
      var stampMin:Number = stampDate.getMinutes();
      var stampSec:Number = stampDate.getSeconds();

      stampReadableFormate = "" + stampDate + "/" + stampMonth + "/" + stampYear + " " + stampHour + ":" + stampMin + ":" + stampSec;

      trace("Date/Time=" + stampReadableFormate);
      }

      return stampReadableFormate;
      }

      now call
      var reportTime:String = this.convertTimeStampToDateAndTime(new Date().getTime());

      Now see the trace out put, what has happened, your timing informations are undefined.
      Looks like it is a bug to me.
      If this issue has already been discussed before I am very sorry by duplicating it.
      Chears
      Naleen
        • 1. Re: Looks like a bug in Date class
          kglad Adobe Community Professional & MVP
          more likely a coding error.
          • 2. Re: Looks like a bug in Date class
            naleen80 Level 1
            what do you mean by coding error...if you can't understand how to do the test wiring up the codes I have shown let me know I will tell you how to do the test.... "coding error"....where...what..? come up with facts rather than just saying... please
            • 3. Re: Looks like a bug in Date class
              Greg Dove Level 4
              It is a coding error

              you are redefining stampDate inside your function/method.

              var stampDate:Date = new Date(timeStampInMilliseconds);

              var stampDate:Number = stampDate.getDate();


              doing it one way around doesn't matter, the other way it does... which is why it doesn't work that way.
              • 4. Re: Looks like a bug in Date class
                Greg Dove Level 4
                so for the actual Day you could use
                stampDay instead of stampDate... (which is your Date instance)
                • 5. Re: Looks like a bug in Date class
                  naleen80 Level 1
                  Thanks for all the responses.
                  I am not redefining stampDate inside your function/method.
                  Please look at it again.
                  I call convertTimeStampToDateAndTime() from out side and in my original post I have written it as following....
                  var reportTime:String = this.convertTimeStampToDateAndTime(new Date().getTime());
                  That is just an example for you to udnerstand it,
                  But what we are doing in our actual application is we read an XML file which has time stamps on each elements as attributes. For an example, if following is the XML line....
                  <element timeStamp='1195770453174' >blaaa</element>

                  In Actionscript we call this method,
                  var reportTime:String = this.convertTimeStampToDateAndTime(1195770453174);
                  So I am not redefining at all.
                  Just do a simple test by user self.

                  My second point is.... If I am redefining a date again with same number of Milliseconds Date class constructor shouldn't care it.
                  For an instance

                  Var myMilliSEC1:Number = new Date().getTime(); // this is constructing today's date and converts to milliseconds
                  var myDate1:Date = new Date(myMilliSEC1); //so this should give me the todays date again...
                  Var myMilliSEC2:Number = myDate1.getTime();

                  Now myMilliSEC1 should equal to myMilliSEC2

                  But that is a different issue.... I am just answering for your comment on "REDEFINING DATE":

                  Anyway thanks for your time and concern.
                  • 6. Re: Looks like a bug in Date class
                    naleen80 Level 1
                    It can be "StampDate" or "StampDay" doesn't matter since it is a declared variable inside a method. Please try to understand the technical side of the issue. It is not a Date instance any way. It is a Number type.
                    • 7. Re: Looks like a bug in Date class
                      Greg Dove Level 4
                      No, I'm sorry but you're wrong.

                      Probably I should have said 'redeclaring' instead of 'redefining' although I'm not sure exactly what the differences are. Please try to understand what I said.

                      You are using the variable name stampDate for your internal Date instance within your method (like this):

                      var stampDate:Date = new Date(timeStampInMilliseconds);


                      You then overwrite it later in the method with the date value obtained from the Date instance.

                      like this:
                      var stampDate:Number = stampDate.getDate();

                      After that stampDate no longer refers to the Date instance and if you try to use a Date instance method on it, it will fail. Try using another name apart from stampDate for the actual Day of the month/date and you will see that it will work. It doesn't have to be 'stampDay' it can be whatever you want... just not stampDate

                      It works ok if its the last thing you do with the Date instance, because you're not using the Date instance after that....;-)


                      • 8. Re: Looks like a bug in Date class
                        kglad Adobe Community Professional & MVP
                        wow gwd, some people just can't see what's in front of their face.
                        • 9. Re: Looks like a bug in Date class
                          Greg Dove Level 4
                          @kglad: lol, I wish I could claim I was immune to that, but I know I'm guilty on occasion. It's always easier for another person to see I guess - it jumped out at me here. What I'd hoped the OP here would do is follow their own advice : s/he was prepared to ask others to test the problem but not prepared to try the first proposed solution. I'm sure if naleen80 had just tried what I suggested earlier s/he would have quickly seen what the problem was.
                          • 10. Re: Looks like a bug in Date class
                            kglad Adobe Community Professional & MVP
                            oh, i used to do that too with code i'd been staring at for hours and sometimes days. but once the error is pin-pointed like you did in your first message, you have to be in denial to miss it.
                            • 11. Re: Looks like a bug in Date class
                              naleen80 Level 1
                              Thanks for all the comments....:)
                              Please try to understand the usage behind this snippet of codes.
                              Date stamp is coming from different place. For an example just imagine a server sends you a XML file with a date stamp which is in miliiseconds.
                              Forget about this line var reportTime:String = this.convertTimeStampToDateAndTime(new Date().getTime());

                              Replace it with var reportTime:String = this.convertTimeStampToDateAndTime(1195770453175);
                              Remember convertTimeStampToDateAndTime() works fine if we get the hour, minute and seconds before we get year, date and month.
                              Please try yourself.
                              We can't understand the role of " kglad" here and his or her contribution....anyway we are only interested in programmers....and their intelligent comments.
                              Thank you

                              • 12. Re: Looks like a bug in Date class
                                naleen80 Level 1
                                Hi all,
                                When you work with an integrated system time stamp can come from anywhere where it was generated. So imagine your action script codes receives this time stamp as an external data. Now you are going to find out which hour, which minute, which second, which day , which month and which year behind that time stamp. Aim of the snippet is so simple to understand and very sorry if I didnt explain it at the beginning.
                                By the way thanks for all the positive comments and support.
                                Just wanted to explain the scenario behind this issue.
                                Have a great day.
                                • 13. Re: Looks like a bug in Date class
                                  Greg Dove Level 4
                                  There was no problem understanding what your code snippet was doing, or what it was intended for. And I (we) understood that new Date().getTime() was test code - there is nothing complicated there. You can also use (new Date()).valueOf()
                                  The intention of your code was clear and there was no need to explain that any further. Your first post provided all the necessary information to solve the issue.

                                  In case others are looking at this topic:
                                  The issue here was a coding error. This does not represent a bug in the Date class.