Zikko's Resources
- 3D to 2D projection - like in the good old days
- Extracting text from PDF files with PHP
- GetJPGInfo - retrieving type, width and height from a JPG image with ASP/VB
- Extended ActionScript Date methods : getWeek() and getDayOfYear()
- Getting the intersection point of two straight lines with Actionscript
- pdf2jpg - Converting PDF to JPG with Photoshop Scripting
- Counting the pages of a PDF document with PHP
- Client-server communication with Flash : Common pitfalls
- Unable to Fork - Running PHP exec/system on IIS
Extended ActionScript Date methods : getWeek() and getDayOfYear()
I've understood that putting numbers on the weeks of the year is typically north european. My spanish teacher was all like "I don't get why you have to do this - give me a DATE for gods sake". Well, we like week numbers here, and we need our Flash applications to retrieve them for us accurately. Since there is no native methods in the ActionScript Date class for it, and the ones I found on other sites seemed hacky and inexact, I decided to write one of my own. As a bonus, I also wrote one for retrieving what day of the year a given date is - getWeek required this.
How does it work?
There is a standard for this, called ISO-8601. It basically states that week one can be defined in one of the following ways. My method is based on the first of them, but they all mean the same thing.
- The week containing January 4th
- The week containing the first thursday of the year
- The first week of the year that has at least four days (this is fuzzy, isn't it?)
- There can be a week 53, if the week following week 52 doesn't qualify for being week one. Week 53 can have days both in January and December.
- The last days of December can be of week one, if one of the days in that week is January 4th.
Have you tested this?
Yeah I have, but please test it some more yourself if you're not sure. I first wrote the code in PHP and compared the result of it to PHP's native code for the same purpose - date('W') and date('z'), then I did the porting to ActionScript.
How do I use it?
var day = new Date();
trace( 'day: '+day );
trace( 'dayOfYear: '+day.getDayOfYear() );
trace( 'week: '+day.getWeek() );
The code
// returns the week of the year of a given Date, starting at one, conforming with ISO-8601
Date.prototype.getWeek = function()
{
var year = this.getFullYear();
// get a Date for January 4th this year
// (week one is the week with this date in it)
var jan4 = new Date( year, 0, 4 );
// get the day of the week for January 4th, monday being zero
var jan4DayOfWeek = ( jan4.getDay() + 6 ) % 7;
// get the day of the year for the first day (monday) of week one that year
// (this may be a negative number if week one started in December the previous year)
var monday1DayOfYear = 3 - jan4DayOfWeek;
// get the day of the year for the current Date
var dayOfYear = this.getDayOfYear();
// the week number is the number of days passed since the first day of week one,
// divided by seven (the number of days per week), rounded down.
var week = 1 + Math.floor( ( dayOfYear - monday1DayOfYear ) / 7 );
// if the week number is below one, it is still the last week of the previous year.
if( week < 1 )
{
// get the Date of the day before January 1st this year
var previousLastDay= new Date( year, 0, 0 );
// get the week number of that day
week = previousLastDay.getWeek();
}
// if the week number is over 52, it may in fact already be week one of the next year
else if( week > 52 )
{
// get a Date of January 4th next year
var nextJan4 = new Date( year+1, 0, 4 );
// get the day of the week for next January 4th, monday being zero
var nextJan4DayOfWeek = ( nextJan4.getDay() + 6 ) % 7;
// get the first day of week one of the next year
var nextMonday1 = new Date( year+1, 0, 4 - nextJan4DayOfWeek );
// if that Date is this Date or before it, it is week one
if( nextMonday1 <= this )
week = 1;
}
return week;
};
// returns the day of the year of a given Date, starting with zero for the first day
Date.prototype.getDayOfYear = function()
{
var year = this.getFullYear();
// start with the day of month of this date, minus one
// (first day of year is day zero)
var dayOfYear = this.getDate()-1;
var month = this.getMonth();
while(month>0)
{
// get the last day of the previous month
var lastDayOfPreviousMonth = new Date(year,month,0);
// add the day of month of that day
// (which is the number of days in that month)
dayOfYear += lastDayOfPreviousMonth.getDate();
month--;
}
return dayOfYear;
};
All content on these pages may be used, copied and modifed freely. Questions or comments may be sent to . Also visit zikko.se.