example: Countdown display including year and month information(AS2)

17 07 2008

[UPDATE] I have uploaded a screenshot of a sample source file for your reference. Please check it out. 🙂 Thanks for the comments.

Very few code examples I could find online have included the number of years and months in them. So I thought I’d come up with my own. It is currently in AS2, leave a comment if you need an AS3 version.

[+] DateUtils class. The function getCountDown takes 2 Date objects and returns a generic object which includes the number of years, months, days, hours, minutes, seconds and milliseconds of the time that is spent to get to the end Date from the start Date. See below.

class DateUtils {
	private static var NUM_MSEC_IN_SECOND:Number = 1000;
	private static var NUM_SEC_IN_MINUTE:Number = 60;
	private static var NUM_MIN_IN_HOUR:Number = 60;
	private static var NUM_HOUR_IN_DAY:Number = 24;
	private static var NUM_MON_IN_YEAR:Number = 12;
	private static var NUM_OF_UNITS:Number = 7; // year, month, day, hours, minutes, seconds, millisesconds

	public static function getCountDown(start:Date,end:Date):Object {
		//trace ("DateUtils, getCountDown of start date:" + start+", end date:"+end);
		if (start == undefined || start == null) start = new Date();
		if (end == undefined || end == null) end = new Date();

		//if the end date is no later than start date, return 0s as results.
		if (end.getTime() - start.getTime()<=0)  return {year:0,month:0,day:0,hour:0,minute:0,second:0,millisecond:0};

		//if the end date is later than start date, continue to do the calculation
		var eY:Number = end.getFullYear();
		var eMon:Number = end.getMonth();
		//
		var start_ary:Array = &#91;start.getMilliseconds(),start.getSeconds(),start.getMinutes(),start.getHours(),start.getDate(),start.getMonth(),start.getFullYear()&#93;;
		var end_ary:Array = &#91;end.getMilliseconds(),end.getSeconds(),end.getMinutes(),end.getHours(),end.getDate(),end.getMonth(),end.getFullYear()&#93;;
		var period_ary:Array = new Array();
		var convsRate_ary:Array = &#91;NUM_MSEC_IN_SECOND,NUM_SEC_IN_MINUTE,NUM_MIN_IN_HOUR,NUM_HOUR_IN_DAY,NaN,NUM_MON_IN_YEAR,0&#93;

		for (var i:Number=0; i<NUM_OF_UNITS; i++)
		{
			period_ary&#91;i&#93;=end_ary&#91;i&#93;-start_ary&#91;i&#93;;
		}
		for (var i:Number=0; i<NUM_OF_UNITS; i++)
		{
			if (period_ary&#91;i&#93;<0)
			{
				period_ary&#91;i+1&#93; -= 1;
				if (convsRate_ary&#91;i&#93; == NaN) convsRate_ary&#91;i&#93; = (eMon>0)?getNumDaysInMonth(eY,eMon-1):getNumDaysInMonth(eY-1,eMon-1+NUM_MON_IN_YEAR);
				period_ary[i] += convsRate_ary[i];
			}
		}
		return {year:period_ary[6],month:period_ary[5],day:period_ary[4],hour:period_ary[3],minute:period_ary[2],second:period_ary[1],millisecond:period_ary[0]}
	}

	public static function getNumDaysInMonth(year:Number,month:Number):Number
	{
		if (month<0 || month>=NUM_MON_IN_YEAR) return 0;
		var firstDayInThisMonth:Date = new Date(year,month,1);
		var firstDayInNextMonth:Date = (month<NUM_MON_IN_YEAR-1) ? new Date(year,month+1,1) : new Date(year+1,month+1-NUM_MON_IN_YEAR,1);
		var numDays:Number = (firstDayInNextMonth.getTime() - firstDayInThisMonth.getTime()) / NUM_MSEC_IN_SECOND / NUM_SEC_IN_MINUTE / NUM_MIN_IN_HOUR / NUM_HOUR_IN_DAY;
		return Math.floor(numDays);
	}
}
&#91;/sourcecode&#93;

&#91;+&#93; CountDownDisplay class. It extends MovieClip and has a TextField on its timeline, with the instance name "txt". The function <em>init</em> takes a target <strong>Date</strong> object to count down (from today) to, and the frequency (in milliseconds) to update the display. The function <em>kill</em> clears the interval.

import mx.utils.Delegate;

class CountDownDisplay extends MovieClip {
	public var txt:TextField;
	private var _intvl:Number;
	private var _frqc:Number;
	private var _tDate:Date;
	private static var DIVIDING_SYMBOL:String = ":";

	private function CountDownDisplay() {}
	public function init(targetDate:Date, frequency:Number):Void
	{
		_tDate = targetDate;
		_frqc = (frequency==0 || frequency==undefined)?100:frequency;
		//this.onEnterFrame = Delegate.create(this,update);
		_intvl = setInterval(Delegate.create(this,update),frequency);
	}
	public function kill():Void
	{
		if (_intvl != undefined) 		clearInterval(_intvl);
	}
	private function update():Void
	{
		clearInterval(_intvl);
		_intvl = undefined;
		var obj:Object = DateUtils.getCountDown(new Date(),_tDate);
		var y:String = obj.year.toString();
		var mon:String = obj.month.toString();
		var d:String = obj.day.toString();
		var h:String = obj.hour.toString();
		var m:String = obj.minute.toString();
		var s:String = obj.second.toString();
		var ms:String = (Math.floor((obj.millisecond) / 10)).toString();

		y = toTwoDigit(y);
		mon = toTwoDigit(mon);
		d = toTwoDigit(d);
		h = toTwoDigit(h);
		m = toTwoDigit(m);
		s = toTwoDigit(s);
		ms = toTwoDigit(ms);

		txt.text = y+DIVIDING_SYMBOL+mon+DIVIDING_SYMBOL+d+DIVIDING_SYMBOL+h+DIVIDING_SYMBOL+m+DIVIDING_SYMBOL+s+DIVIDING_SYMBOL+ms;
		updateAfterEvent();
		_intvl = setInterval(Delegate.create(this,update),_frqc);
	}

	private function toTwoDigit ( str:String ) : String
	{
		var new_str:String = str;
		switch (true)
		{
			case Boolean(str.length==0):
				new_str = "00";
				break;
			case Boolean(str.length==1):
				new_str = "0"+str;
				break;
			case Boolean(str.length>2):
				new_str = "**";
				break;
		}
		return new_str;
	}
}

There you go. I’d be happy to send you a .fla file as an example. Let me know please.

Advertisements

Actions

Information

12 responses

1 10 2008
seeta

would it be possible if I could have the fla. file for example: Countdown display including year and month information(AS2)

29 10 2008
lulumOriss

Hi, could you please send me the .fla file ?
I tried to implement your class but I didn’t understand how to instanciate it.

Thanks.

lulu.

7 11 2008
source - Countdown display including year and month information(AS2) « Interactive Section

[…] MovieClip’s linkage links to the Class “CountDownDisplay”, which can be found in my previous post. In the actionscript layer, I have a line of code as the […]

18 11 2008
vedran

can i ask tyou for the .fla file ?
thx man!

21 11 2008
Hu

Sent. Please check your email.

13 12 2008
Bookmarks about Interactive

[…] – bookmarked by 4 members originally found by kfisch on 2008-11-06 example: Countdown display including year and month information(AS2) […]

15 12 2008
Recent Links Tagged With "as2" - JabberTags

[…] by erincolbert on Sun 07-12-2008 AS2/AS3: CustomEase Saved by jimb0tr0nic on Sun 30-11-2008 example: Countdown display including year and month information(AS2) Saved by Rakeweru on Sun 30-11-2008 Papervision AS2 reflected bitmap Saved by stuffins on Tue […]

24 10 2009
Ghad

can I have the source file plz?

15 01 2010
FlashNewbie

Would it be possible if I could have the fla, in AS3 if you have it. I too am looking for month & year to be inlcuded into the countdown. Thx

16 01 2010
Flash_Newbie

Please send me the .fla file. I would like to review it as my code isn’t working.

Thank you.

11 03 2010
My first Adobe Air Application | Elaine is bored now.

[…] step was to get an accurate countdown mechanism. I found this Countdown example which was a good starting point to create a Countdown class in AS3. The basic idea is that it takes […]

13 12 2010
Anthony

Please send me the fla file, that would be awesome. anthbot@gmail.com
Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: