Flex Date Utils – dayOfYear and weekOfYear
2 more methods for today’s post. These may or may not be useful to people. I don’t think I have yet to use them in any of my code (maybe one time using the ColdFusion equivalents), but I thought I would translate them to Flex all the same.
/** * Gets the ordinal value or day of the year * * @param date The date for which to get the day of the year * * @return A number representing the day of the year, 1 to 365 or 366 for a leap year */ public static function dayOfYear( date:Date ):Number { // add one as it has to include first of year return DateUtils.dateDiff( DateUtils.DAY_OF_MONTH, new Date( date.fullYear, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ), date ) + 1; }
This method takes a date and calculates the ordinal day of the year that that date represents, from 1 to 365 or 366 in a leap year. This just uses the dateDiff method to subtract the number of days between the 1st of the year, and the date parameter (and adds one to include the first day of the year).
To implement, first import the package:
import com.flexoop.utilities.dateutils.DateUtils;
then:
private var _dayOfYear:Number = DateUtils.dayOfYear( new Date() );
/** * Gets the week of the year * * @param date The date for which to get the week of the year * * @return A number representing the week of the year, 1 to 53 ( as there are slightly more than 52 weeks of days in a year) */ public static function weekOfYear( date:Date ):Number { return Math.ceil( DateUtils.dayOfYear( date ) / 7 ); }
This method works very similarly to the dayOfYear method except that it calculates which week of the year the date falls in, and returns a number from 1 to 53.
To implement, first import the package:
import com.flexoop.utilities.dateutils.DateUtils;
then:
private var _weekOfYear:Number = DateUtils.weekOfYear( new Date() );
All methods in this series are describing code in the FlexDateUtils package I put on Google code.
Hey, I needed DOY values for an RIA I’m working on but couldn’t find anything pre-built within Flex. Your code worked great and has helped me a lot!
Excellent job!! Keep it up!!
Thanks Raj! Glad it’s getting some use.
One other thing to mention…I and a co-programmer are currently working on a solution to the daylight saving time problem that occurs in the DateUtils library. Flash player will assign 23 hours to the “spring ahead” day of the year, but this then causes all dateDiff calculations between the “spring ahead” and “fall back” days to be one hour off (which is what I’m using to calculate the day of year). Thus, when I calculate the dayOfYear for a date falling between those dates, it is actually one day off.
Nate Beck is actually working on fixing this with a modification to the Date class that will allow the setting of a timezone. If we could set the timezone to Arizona (which does not have DST), then all of the dates will be automatically fixed. Stay tuned as this is high on my priority of fixes. For now, you could use the DaylightSavingTimeUS class that is also in the DateUtils library to check whether the date “isDST” (which by my implementation means it falls between “spring ahead” and “fall back”) and then add 1 day to the calculated DofY. This will then fix the dates being off by one. You can verify with this site, but that should definitely fix the problem.
Hi Gareth,
i just wanted to include the function weekOfYear() in one of
my projects, but as it works today i cannot use it.
For instance the function tells me that the two dates 2009-08-20 and
2009-08-14 both belong to the same week, which is definitely not true.
Couldn’t you please add another function to your library, which does
take care of real weeks, and not just ‘/ 7′ the dayOfYear ?
I’ve just noticed that this function is included in Flex 4, so i try
to have a look on the source code.
Thanks a lot.
Regards
valley
Btw: With that JavaScript function adapted to as3 it works fine:
http://blog.dofy.net/?p=67
Hey Valley,
Someone else posted a few weeks back that my calculations for weekOfYear was incorrect. I didn’t realize there was some logic behind it rather than just trying to divide dayOfYear by 7. I started working on the fix this past weekend, but got pulled away.
Thanks for the links. I’ll (hopefully) be able to check it out soon. Certainly makes it easier when someone sends me something with a code fix
I have updated the weekOfYear method so it now returns the correct weekOfYear (using the ISO standard). I’ve also added unit tests to the library, so any changes I may make should be caught before they’re delivered out.
Thank you, Gareth.
I’ll check it out soon and test it within my project.
Regards
valley
Hi,
thanks for your great job.
Just one question, how can i get a date from day of year ?
Cheers
I don’t think I’ve got that method in the date utils yet, but I will definitely look into adding it. Thanks for the idea.
Thanks Gareth,
do you have an idea to implement this functionality ? i really need of this method.
Cheers
An idea, with VB.NET you can write this :
Dim d As New DateTime(2008, 1, 1) ‘ 1er janvier
d = d.AddDays(numJour – 1)
And it’s work, now it should translate into Action Script.
Yup, that should work fine in Actionscript. To do that with the library, you would just use dateAdd.
DateUtils.dateAdd( DateUtils.DAY_OF_MONTH, ( dayOfYearNumber – 1 ), new Date( 2008, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ) );
It’s work, you are fantastic, thanks.
Cheers