Home > Flex, FlexDateUtils, code, library > Flex Date Utils – dayOfYear and weekOfYear

Flex Date Utils – dayOfYear and weekOfYear

December 29th, 2008 Gareth Leave a comment Go to comments

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.

  1. Raj
    January 14th, 2009 at 20:09 | #1

    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!!

  2. Gareth
    January 14th, 2009 at 20:15 | #2

    Thanks Raj! Glad it’s getting some use.

  3. Gareth
    January 14th, 2009 at 20:37 | #3

    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.

  4. valley
    August 24th, 2009 at 10:16 | #4

    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

  5. valley
    August 24th, 2009 at 11:13 | #5

    Btw: With that JavaScript function adapted to as3 it works fine:
    http://blog.dofy.net/?p=67

  6. Gareth
    August 24th, 2009 at 12:42 | #6

    Hey Valley,
    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 :) 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.

  7. Gareth
    August 26th, 2009 at 14:57 | #7

    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.

  8. valley
    August 27th, 2009 at 09:15 | #8

    Thank you, Gareth.
    I’ll check it out soon and test it within my project.

    Regards
    valley

  9. tafkap
    December 24th, 2009 at 04:28 | #9

    Hi,

    thanks for your great job.

    Just one question, how can i get a date from day of year ?

    Cheers

  10. Gareth
    December 24th, 2009 at 21:44 | #10

    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.

  11. tafkap
    December 28th, 2009 at 03:10 | #11

    Thanks Gareth,

    do you have an idea to implement this functionality ? i really need of this method.

    Cheers

  12. tafkap
    December 28th, 2009 at 03:30 | #12

    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.

  13. Gareth
    December 28th, 2009 at 08:04 | #13

    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 ) );

  14. tafkap
    December 28th, 2009 at 08:18 | #14

    It’s work, you are fantastic, thanks.

    Cheers

  15. April 13th, 2010 at 15:29 | #15

    This is pretty cool. Do you have or know of anything that does the opposite? e.g. takes year + day in year and returns a Date object?

    toDate(2012,360) would return a Date with the 360th day in 2012

  16. Gareth
    April 13th, 2010 at 20:48 | #16

    Glad you like it. Very simple to do what you need using the dateAdd method :)

    DateUtils.dateAdd( DateUtils.DAY_OF_MONTH, new Date( 2011, DateUtils.monthAsNumber( DateUtils.DECEMBER ), 31 ), 360 );

    This should get you to the 360th day of 2012.

  17. April 14th, 2010 at 10:15 | #17

    @Gareth
    Cool thanks Gareth! This is what I ended up doing:

    public static function dayInYear2Date(year:int, dayInYear:int):Date
    {
    return DateUtils.dateAdd( DateUtils.DAY_OF_MONTH, dayInYear
    , new Date(year-1, 11/*dec*/, 31));
    }

  1. May 31st, 2010 at 01:08 | #1