Link to home
Start Free TrialLog in
Avatar of marmaxx
marmaxx

asked on

Getting the Week Number for the Year Given a Date

I am trying to find a simple way to get the week number for a year, given a data in the format dd/mm/yyyy (e.g. 07/01/2006). The year will not necessarily be the curent year, it could be a past or future year. (This is what's giving me the problem).

Thanks!

Avatar of joechina
joechina

Try this

static int weekOfYear(DateTime dt)
{
     return (int)((int)(dt.DayOfYear + DateTime.ParseExact(dt.Year.ToString(), "yyyy", null).DayOfWeek) / 7 + 1);
}

Week is from Sunday to Saturday.

ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marmaxx

ASKER

Thanks!