9 Replies Latest reply on May 6, 2008 6:38 AM by tookiebunten

    Date and Time

    tookiebunten
      Hi

      Is there an easy way to display the users system time and date?

      Thanks
        • 1. Re: Date and Time
          jylaxx Level 1
          date = new Date() ;

          then format the date object with DateFormatter if necessary.

          add a label to the stage and set the text property.

          Is that your question ?
          • 2. Re: Date and Time
            tookiebunten Level 1
            Hi

            Sorry for being dump but I'm new to flex and actionscript have only been using it for about 3 weeks and haven't had a course yet. How do I use that and does it also show the time and will it update on its own?

            Thanks
            • 3. Re: Date and Time
              jylaxx Level 1
              Well try first without the DateFormatter, just set the text property of the Label to date.toString().

              To refresh the value you have to use a Timer and update the date in the TIMER event. To get the new date just recreate a new Date object.

              If all is working then look at the documentationi about DateFormatter.
              • 4. Re: Date and Time
                tookiebunten Level 1
                I'm getting the date displayed ok but I'm struggling to get a formatter to work can you check my code and tell me where I'm going wrong?

                I'm probably doing something silly thanks

                • 5. Re: Date and Time
                  jylaxx Level 1
                  the wrong code is not here. What you post is correct. But I don't know where you call the formatDisplayTime method ???
                  • 6. Re: Date and Time
                    tookiebunten Level 1
                    This is my full code. Be nice I'm just learning and the code might be all other the place. Thanks for having a look.

                    • 7. Re: Date and Time
                      jylaxx Level 1
                      OK !
                      1- You never call formatDisplayTime.
                      2- dateTime is defined once at the creation of the application and then it doesn't change. Making this variable bindable is useless.

                      You have to use a timer as I told you :

                      (1) Add this code

                      [Bindable]
                      private var strNow : String ;

                      private var timer : Timer ;

                      /*private var dateTime:Date = new Date();
                      private function formatDisplayTime(item:Date):String
                      {
                      return timeDisplayFormatter.format(item);
                      }*/

                      private function init() : void
                      {
                      murdochRoomData.send() ;
                      edisonRoomData.send() ;
                      monierRoomData.send() ;
                      bellRoomData.send() ;

                      timer = new Timer( 1000 ) ;
                      timer.addEventListener( TimerEvent.TIMER, onTimer ) ;
                      timer.start() ;
                      }

                      private function onTimer( event : TimerEvent ) : void
                      {
                      strNow = timeDisplayFormatter.format( new Date() ) ;
                      }

                      (2) Call the init() function in the Application.creationComplete event : creationComplete="init();"

                      (3) Replace the label binding : <mx:Label text="{strNow}" fontWeight="bold"/>

                      That's all !
                      • 8. Re: Date and Time
                        jylaxx Level 1
                        Well I keep binding as example, but it is not necessary in this case.

                        Add an id to the date-time label and set the text property of the label in the onTimer event :

                        private function onTimer( event : TimerEvent ) : void
                        {
                        dateTimeLabel.text = timeDisplayFormatter.format( new Date() ) ;
                        }

                        <mx:Label id="dateTimeLabel" fontWeight="bold"/>
                        • 9. Re: Date and Time
                          tookiebunten Level 1
                          thanks
                          for that