PC Review


Reply
Thread Tools Rate Thread

Calculate week number of given date

 
 
=?Utf-8?B?ZHVyZGk=?= durdisoft.ch>
Guest
Posts: n/a
 
      24th Feb 2004
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

 
Reply With Quote
 
 
 
 
Pete Vickers [eMVP]
Guest
Posts: n/a
 
      24th Feb 2004
Hi,
try
Dim intWeekno As Integer

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

HTH

Pete



--
Pete Vickers
Microsoft Windows Embedded MVP
HP Business Partner
http://www.gui-innovations.com

"durdi durdisoft.ch>" <durdek.m<at> wrote in message
news:13F421DE-19F9-430D-BC17-(E-Mail Removed)...
> 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 help
>



 
Reply With Quote
 
Neil Cowburn [MVP]
Guest
Posts: n/a
 
      24th Feb 2004
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


durdi durdisoft.ch> <durdek.m wrote:
> 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 help
>

 
Reply With Quote
 
Boris Nienke
Guest
Posts: n/a
 
      24th Feb 2004
On Tue, 24 Feb 2004 13:18:22 +0000, Neil Cowburn [MVP] wrote:

> 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
 
Reply With Quote
 
Boris Nienke
Guest
Posts: n/a
 
      24th Feb 2004
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
 
Reply With Quote
 
Romain TAILLANDIER
Guest
Posts: n/a
 
      24th Feb 2004
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


"Boris Nienke" <(E-Mail Removed)> a écrit dans le message de news:
1ipypf6ugmwqr$.(E-Mail Removed)...
> On Tue, 24 Feb 2004 13:18:22 +0000, Neil Cowburn [MVP] wrote:
>
> > 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



 
Reply With Quote
 
Mark Johnson
Guest
Posts: n/a
 
      25th Feb 2004
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 Removed)

"durdi durdisoft.ch>" <durdek.m<at> schrieb im Newsbeitrag
news:13F421DE-19F9-430D-BC17-(E-Mail Removed)...
> 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 help
>



 
Reply With Quote
 
Neil Cowburn [MVP]
Guest
Posts: n/a
 
      25th Feb 2004
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-servi...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

Mark Johnson wrote:
> 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 Removed)
>
> "durdi durdisoft.ch>" <durdek.m<at> schrieb im Newsbeitrag
> news:13F421DE-19F9-430D-BC17-(E-Mail Removed)...
>
>>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 help
>>

>
>
>

 
Reply With Quote
 
Sandeep Prabhakar [MSFT]
Guest
Posts: n/a
 
      19th Mar 2004
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate a date knowing the week number and year =?Utf-8?B?U2FyYWg=?= Microsoft Access Form Coding 7 2nd Aug 2006 06:07 PM
How can I calculate the week number from a date, eg 21/06/05 = 26 =?Utf-8?B?Y2VsNTA0?= Microsoft Excel Worksheet Functions 2 3rd Aug 2005 08:14 PM
Calculate date from week number =?Utf-8?B?UnVzdGFu?= Microsoft C# .NET 2 19th Oct 2004 02:25 PM
formula to calculate the week number from a date? luis Microsoft Excel Discussion 4 2nd Sep 2004 12:53 PM
How do you calculate the week number in a year of a given date? Mike Microsoft Access Forms 2 21st Nov 2003 12:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:42 PM.