5 Replies Latest reply on Jul 24, 2009 10:01 PM by kglad

    Using AS2 to get time and change based on result

    jeremiahtrue Level 1

      I need to make an edit to a flash projector that was built some time ago that changes what is playing based on the time of day. It is used to advertise breakfast or lunch/dinner for a restaurant. Previously we had set it up so that the time change happened at 5 in the morning and 10 in the morning. The code is below. The restaurant wants to change the time switch to be on the half hour now. I know how to pull the hours and minutes to do this.

       

      This is the code I have been using

       

      var currentHours:Date = new Date();
      var currentMin:Date = new Date();

      timeInHours = currentHours.getHours();
      timeInMin = currentMin.getMinutes();

      var now:Date = new Date();
      shownow = now.setMilliseconds;


      if (timeInHours > 4 && timeInHours < 11 ){
      breakfast_mc._visible = true;
      breakfast_mc.play();
      dinner_mc._visible = false;
      dinner_mc.stop();
      //trace(timeInHours);
      } else {
      breakfast_mc._visible = false;
      breakfast_mc.stop();
      dinner_mc._visible = true;
      dinner_mc.play();
      //trace(timeInHours);
      }
      trace(timeInHours);
      trace(timeInMin);
      stop();
      The breakfast and dinner movie clips have this code in them as well, so than at the end of each cycle, it checks the time and will switch.
      I found this code on Flash Kit and wanted to try and modify it, My hope was to get it to generate a 24hr time 1604 or 1029 that I could then do a < / > comparison with, but this function adds the numbers together so 4:15 PM = 31 and 12:19 = 31 also. Does anyone have any suggestions about how I can do this? I need the time to change at 5 AM and 10:30 AM.  There is a possibility that they may want to extend it out to include day of the week as well. RIght now I just need fractional hours to work though.
      function checkTime()
      {
      var dat = new Date();
      var hr = dat.getHours(); // this is 24-hour format already...
      var mn = dat.getMinutes();
      // round to most recent 15 minute boundary
      //mn = Math.floor(mn/15) * 15;

      // OPTIONAL - add leading zeros to hr and minute
      if (hr < 10)
      hr = "0" + hr;
      if (mn < 10)
      mn = "0" + mn; // remove these last 4 lines if leading zeros not wanted

      return hr + mn;
      }

      // example usage:
      time = checkTime();
      trace( time );
      Any and all suggestions are welcome. Thanks in advance!
        • 1. Re: Using AS2 to get time and change based on result
          kglad Adobe Community Professional & MVP

          if you want to do something when the user's time is between 5am and 10:30am use:

           

          var d:Date=new Date();

           

          if((d.getHours()>=5 && d.getHours()<10) ||(d.getHours()==10&&d.getMinutes<=30)){

          //do whatever

          }

          • 2. Almost there, still not working though
            jeremiahtrue Level 1

            Hi Kglad,

            Thanks for your response, I am trying the code now and I am having some difficulty with it. This is probably so simple, but I can't see it.

             

            var d:Date=new Date();

             

            if ((d.getHours()>=5 && d.getHours()<10) || (d.getHours()==10 && d.getMinutes<=30)){

            breakfast_mc._visible = true;

            breakfast_mc.play();

            dinner_mc._visible = false;

            dinner_mc.stop();

            trace("it is breakfast time");

            } else {

            breakfast_mc._visible = false;

            breakfast_mc.stop();

            dinner_mc._visible = true;

            dinner_mc.play();

            trace("it is lunch time");

            }

             

            stop();

             

            Once it is past 10:30 I want the dinner movie clip to play and the breakfast clip to stop and disappear. After 5 in the morning, I want the dinner clip to stop and the breakfast one to start.

             

            I've tried < and > with minutes and neither seems to work. The hours work, I set up a test clip with this script in it and when I change my computer clock, I can make this switch back and forth with hours but the minutes still do not factor in.

             

            Thanks

            • 3. Re: Almost there, still not working though
              Ned Murphy Adobe Community Professional & MVP

              I may just be a syntax error, missing the parentheses...

               

              d.getMinutes() <= 30

              1 person found this helpful
              • 4. It Works!
                jeremiahtrue Level 1

                Thanks to both of you for your help, this works perfectly!

                • 5. Re: It Works!
                  kglad Adobe Community Professional & MVP

                  you're welcome.