Hide
Meet people who work on similar things as you – get help if you need it Join Siafoo Now or Learn More

Find date's day within year Atom Feed 1

In Brief Unlike many other languages, ActionScript 3 does not have a method for finding a date's day within a year. For example, February 10, 2008, is the 41st day in the year.
# 's
 1function getDayOfYear(date:Date):Number {	
2 var monthLengths:Array = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
3
4 // A leap year is divisable by 4, but not by 100 unless divisable by 400.
5 if (((date.getFullYear() % 4 == 0) && (date.getFullYear() % 100 != 0)) || (date.getFullYear() % 400 == 0)) {
6 monthLengths[1] = 29;
7 trace ("leap year");
8 }
9
10 var dayInYear = 0;
11
12 // get day of year up to month
13 for (var i:Number = 0; i < date.getMonth(); i++) {
14 dayInYear += monthLengths[i];
15 }
16
17 // add day inside month
18 dayInYear += date.getDate();
19
20 // Start counting on 0 (optional)
21 // dayInYear--;
22
23 return dayInYear;
24}
25
26trace(getDayOfYear(new Date("02/10/2008")));
27// returns 41

Unlike many other languages, ActionScript 3 does not have a method for finding a date's day within a year. For example, February 10, 2008, is the 41st day in the year.