A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: countdown to weekend

  1. #1
    Junior Member
    Join Date
    Jun 2003
    Posts
    10

    countdown to weekend

    I'm trying to build a countdown from 9am on Monday mornings showing days,hours, minutes until 5.15 every Friday, at which point the display will change to 'It's the weekend!'.

    I've got the code for a straightforward countdown to a specified date and time and when the end time is reached I can change the display.

    My problem is this method works only once because the end time is fixed. I'm stumped as to how to make this run regularly every week and the various tutorials I've read don't help me.

    Any suggestions would be really appreciated. Thx.

  2. #2
    Always Without Crutches!
    Join Date
    Feb 2005
    Location
    On The Interweb Thingy
    Posts
    44
    Big apology for the major necro post....but I was looking for the answer to this very question and am unable to find one. Any help would be greatly appreciated!

  3. #3
    Always Without Crutches!
    Join Date
    Feb 2005
    Location
    On The Interweb Thingy
    Posts
    44
    Okay....progress! I post on another forum gotoAndLearn() and I posed a similar question there! Someone has come back with some script....of course more work needs to be done...but share and share alike eh!?

    Quote Originally Posted by drukepple
    I'm not familiar with Senocular's tutorial, but I'm guess you can do this relatively easily.

    The Date object is key. Create a new Date like this:
    Code:
    var currentDate:Date = new Date();
    And you have the current date and time stored in a var called currentDate. Keep in mind that this is a "snapshot" of a a date and time, not a continually updating "currentDate."

    Now, you can create another new Date like this:
    Code:
    var otherDate:Date = new Date(2006, 4, 20, 17, 0, 0, 0);
    And you have a "snapshot" of the date/time of 5:00 PM on Friday, May 20, 2006.

    With these two dates, you can get the difference between hours, days, whatever, and probably work out your countdown from there. The idea would be to store the destination time in a variable, and continually get a new Date() every time you need to update the countdown timer.

    The trick, however, is getting the next Friday, and not a hard coded one.

    The following create a new Date, set to the current date, and then increments it by one day until it's Friday:

    Code:
    // Create a date to be Friday, but start out as the current date.
    var fri:Date = new Date();
    
    // use getDay() to see if it's 5 (friday).  If not, add on to the 
    // date (e.g. May 15 becomes May 16).
    while (fri.getDay() != 5) {
    	trace(fri);
    	fri.setDate(fri.getDate()+1);
    }
    // Set the Time to 5:00:00:000 PM
    fri.setHours(17);
    fri.setMinutes(0);
    fri.setSeconds(0);
    fri.setMilliseconds(0);
    
    trace(fri);
    I put some traces in there so you can see the date advance itself. At the end of this code block, the var fri holds a timestamp of the next Friday, 5:00 PM. Can you take it from there?
    I'm having a crack at it myself...but if anybody out there is better at AS than me (that'll be most of ya's) then feel free to jump in any time with advice and ideas!

  4. #4
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    OK, same concept, but these lines just make me cringe!
    Code:
    // use getDay() to see if it's 5 (friday).  If not, add on to the
    // date (e.g. May 15 becomes May 16).
    while (fri.getDay() != 5) {
    	trace(fri);
    	fri.setDate(fri.getDate()+1);
    }
    NASTY! Try this
    Code:
    function getNextWhenever( dow:Number, hours:Number, minutes:Number, seconds:Number, ms:Number ):Date {
    	var c:Date = new Date(); // used to check later
    	var d:Date = new Date(); // used for calculations
    
    	// make it the day of the week we want
    	// 0 - Sunday, 1 - Monday ... 6 - Saturday
    	d.setDate( d.getDate() + ( dow - d.getDay() ) );
    	d.setHours( hours );
    	d.setMinutes( minutes );
    	d.setSeconds( seconds );
    	d.setMilliseconds( ms );
    
    	if( c.valueOf() > d.valueOf() ) { // date is in the past
    		d.setDate( d.getDate() + 7 ); // make it next week then
    	}
    
    	return d;
    }
    
    // Some shortcut functions, because I'm lazy
    function  getNextWeekend():Date { return getNextWhenever( 5, 17, 15, 0, 0 ); }
    function getNextWorkWeek():Date { return getNextWhenever( 1,  9,  0, 0, 0 ); }
    
    function breakDown( t:Number ):String {
    	var s:String = "";
    	t = t / 1000;
    	
    	if( t >= 86400 ) {
    		s = s + ( Math.floor( t / 86400 ) ) + " days";
    		t %= 86400;
    	}
    	if( t >= 3600 ) {
    		s = s + " " + ( Math.floor( t / 3600 ) ) + " hours";
    		t %= 3600;
    	}
    	if( t >= 60 ) {
    		s = s + " " + ( Math.floor( t / 60 ) ) + " minutes";
    		t %= 60;
    	}
    	if( t ) {
    		var c:String = ( Math.floor( t * 1000 ) ).toString();
    		c = c.substr( 0, -3 ) + '.' + c.substr( -3 );
    		if( t < 1 ) {
    			c = '0' + c;
    		}
    		s = s + " " + c + " seconds";
    	}
    	
    	if( s.charAt( 0 ) == ' ' ) {
    		s = s.substr( 1 );
    	}
    
    	return s;
    }
    
    onEnterFrame = function() {
    	var d:Date = new Date(); // right now
    	var we:Date = getNextWeekend(); // next occurance of Friday, 17:15
    	var ww:Date = getNextWorkWeek(); // next occurance of Monday, 09:00
    	// Is it the weekend?
    	var weekend:Boolean = ( ww.valueOf() < we.valueOf() );
    	// Figure out how long until the next event
    	var ourDiff:Number = ((weekend)?ww.valueOf():we.valueOf()) - d.valueOf();
    
    	// Change the lines below to suit your own needs
    	// This assumes you have a text box on stage with the name of testTextBox
    	var test:String = breakDown( ourDiff );
    	if( weekend ) {
    		_root.testTextBox.text = "It's the weekend!\n" + test + "\nuntil next week";
    	} else {
    		_root.testTextBox.text = "It's the work week.\n" + test + "\nuntil the weekend!";
    	}
    }
    Last edited by wombatLove; 05-20-2006 at 02:25 AM. Reason: Minor change to function breakDown
    If you can read this, you're in the right place.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center