Calculate week number of given date

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

How do I calculate the week number from an given date, e.g
19.02.2004 --> Week 8

Is there an function/methde in the cf

Thanks for hel
 
Hi,
try
Dim intWeekno As Integer

intWeekno = CInt(DateDiff("d",Startdate, Date.Today, VbSunday) / 7) + 1

HTH

Pete
 
Try something like this:

public double WeekOfYear(DateTime date)
{
DateTime FirstOfYear = new DateTime(today.Year,1, 1);
return Math.Floor((date - FirstOfYear).TotalDays / 7) + 1;
}

This assumes Week 1 starts on January 1st.

HTH
Neil

--
Neil Cowburn, MVP
Co-founder, OpenNETCF.org
Technologist, Content Master Ltd
Microsoft .NET Compact Framework MVP

www.opennetcf.org | www.contentmaster.com
 
Try something like this:

public double WeekOfYear(DateTime date)
{
DateTime FirstOfYear = new DateTime(today.Year,1, 1);
return Math.Floor((date - FirstOfYear).TotalDays / 7) + 1;
}

This assumes Week 1 starts on January 1st.

not sure - but this is not correct, isnt' it?
IIRC there is at least one rule ... but can't remember it... but i think
the first of Jan. may not the start of week 1

Boris
 
Some Delphi-Procedures... don't know - but some things may be easier with
C# :

function dateWeekOfYear(D: TDateTime): Integer; { Armin Hanisch }
const
t1: array[1..7] of ShortInt = ( -1, 0, 1, 2, 3, -3, -2);
t2: array[1..7] of ShortInt = ( -4, 2, 1, 0, -1, -2, -3);
var
doy1,
doy2 : Integer;
NewYear : TDateTime;
begin
NewYear:=dateBeginOfYear(D);
doy1 := dateDayofYear(D) + t1[DayOfWeek(NewYear)];
doy2 := dateDayofYear(D) + t2[DayOfWeek(D)];
if doy1 <= 0 then
Result := dateWeekOfYear(NewYear-1)
else if (doy2 >= dateDayofYear(dateEndOfYear(NewYear))) then
Result:= 1
else
Result:=(doy1-1) div 7+1;
end;


function dateBeginOfYear(D: TDateTime): TDateTime;
var
Year,Month,Day : Word;
begin
DecodeDate(D,Year,Month,Day);
Result:=EncodeDate(Year,1,1);
end;

function dateDayOfYear(D: TDateTime): Integer;
begin
Result:=Trunc(D-dateBeginOfYear(D))+1;
end;

Boris
 
Note that i don't if this is true in WCE, but it is true in palm OS :

"the first week of a year is the first week where there is more day in
january than in december."

eq : if the 1st january is monday tuesday wenesday or thursday, this week is
the first (resp 7, 6, 5, and 4 days in the week are in january)

else if the 1st january is a friday, saturday or sunday (resp 3, 2 or 1 days
in january and consequently 4, 5 and 6 day in december) this week is the
last of previous year. so the first week is the one which start on monday 2,
3 or 4 january.


hopes it's help.
ROM
 
Please note that these rules are also Culture Specific,
which means that some European cultures have differnt way of determing
what is week 1 or week 53.

I don't remember what it was (very long time ago) it it may have something
to to with first Day of Week.
(Europe = Monday)

Mark Johnson, Berlin Germny
(e-mail address removed)
 
OK, so I did some research into this and found there's an ISO standard
for defining Week 1 of any given year and for defining the first day of
the week. See the url below for more details:

http://www.iso.ch/iso/en/prods-services/popstds/datesandtime.html?printable=true

Using this, I came up with the following algorithm:

// Calculates the Week Number in accordance to ISO-8601
public int GetWeekNumber(DateTime dt)
{
int year = dt.Year;

// Check that the date is or is after December 29.
if (dt >= new DateTime(year, 12, 29))
{
week1 = GetWeekOneDate(year + 1);
if (dt < week1)
{
week1 = GetWeekOneDate(year);
}
}
else
{
week1 = GetWeekOneDate(year);
if (dt < week1)
{
week1 = GetWeekOneDate(--year);
}
}

return ((dt - week1).Days / 7 + 1);
}

public DateTime GetWeekOneDate(int year)
{
// Get the date for Jan-4 for the given year
DateTime date = new DateTime(year, 1, 4);

// Get the ISO-8601 day number for this date 1==Monday, 7==Sunday
int dayNum = (int)date.DayOfWeek; // 0==Sunday, 6==Saturday
if(dayNum == 0)
dayNum = 7;

// Return the date of the Monday that is less than or
// equal to this date
return date.AddDays(1 - dayNum);
}



--
Neil Cowburn, MVP
Co-founder, OpenNETCF.org
Technologist, Content Master Ltd
Microsoft .NET Compact Framework MVP

www.opennetcf.org | www.contentmaster.com
 
This is very similar to previous replies, but you don't need to use the
first day of the year.

int weekNumber = (((new DateTime(year,month,day)).DayOfYear)/7) + 1;

Sandy

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top