user601935
user601935

Reputation: 133

How to get the week number in the month of a Date - AS3 (Actionscript 3)

How would I get the week number in the month of a Date?

I can retrieve the week number in a year w/ getWeekOfTheYear (d:Date) but a calculation based around that will prove difficult - any help is greatly appreciated

Returned value I would expect would typically range from [0 - 4]

Upvotes: 0

Views: 417

Answers (2)

Andre Fellipe Martins
Andre Fellipe Martins

Reputation: 11

Take a look at this aproach:

Get current calendar week

Use the function week_num to get the week number passing a date object as you wanted.

Upvotes: 1

geesys
geesys

Reputation: 11

You have to account for months starting in the middle of the week, where e.g. the 1st is a Thursday.

Get the week day number of the months 1st day minus one. Add that to your current date, divide by 7, floor it, add one to the result: That's the months week (zero-based) of that day.

// be carful to provide month zero-based like AS need it to be
private function monthWeekOfDay(year:int, month:int, day:int, weekStartsOnMonday:Boolean = false):int
{
    var firstDay:int = new Date(year, month, 1).getDay();
    if (weekStartsOnMonday)
        firstDay = (firstDay + 6) % 7;
    firstDay--;

    return Math.floor((new Date(year, month, day).getDate() + firstDay) / 7);
}

Upvotes: 0

Related Questions