Les Snippets

Connexion

Recuperer le numero de la semaine

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 25/03/2006 23:36:20 et initié par EBArtSoft [Liste]
Vue : 12018
Catégorie(s) : Maths
Langages dispo pour ce code :
- VB6, VBA
- PHP 3, PHP 4, PHP 5
- C# 1.x, C# 2.x
- VB6
- VB 2005, VB.NET 1.x
- C# 1.x, C# 2.x
- VB6, VBA
- Voir tous les langages pour ce code snippet



Langage : C# 1.x , C# 2.x
Date ajout : 30/04/2006
Posté par Bidou [Liste]
public static int ISOWeekNumber(DateTime dt) 
{
int yyyy = dt.Year; 
int mm = dt.Month;
int dd = dt.Day; 
// Declare other required variables
int jan1WeekDay; 
int weekNumber = 0;
int weekDay = 0; 
int i, j, k, l, m, n;
int[] mnth = new int[12] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 
int yearNumber;
// Set DayofYear Number for yyyy mm dd

int dayOfYearNumber = dd + mnth[mm - 1]; 
// Increase of Dayof Year Number by 1, if year is leapyear and month is february
if ((IsLeapYear(yyyy)) && (mm == 2)) dayOfYearNumber += 1; 
// Find the Jan1WeekDay for year 

i = (yyyy - 1) % 100;

j = (yyyy - 1) - i;

k = i + i / 4;

jan1WeekDay = 1 + (((((j / 100) % 4) * 5) + k) % 7);

// Calcuate the WeekDay for the given date

l = dayOfYearNumber + (jan1WeekDay - 1);

weekDay = 1 + ((l - 1) % 7);

// Find if the date falls in YearNumber set WeekNumber to 52 or 53
if ((dayOfYearNumber <= (8 - jan1WeekDay)) && (jan1WeekDay > 4)) 
{

yearNumber = yyyy - 1;
if ((jan1WeekDay == 5) || ((jan1WeekDay == 6) && (jan1WeekDay > 4))) weekNumber = 53;else weekNumber = 52; 
}

else yearNumber = yyyy;
// Set WeekNumber to 1 to 53 if date falls in YearNumber

if (yearNumber == yyyy) 
{
if (IsLeapYear(yyyy) == true) m = 366; 
else m = 365;
if ((m - dayOfYearNumber) < (4 - weekDay)) 
{

yearNumber = yyyy + 1;

weekNumber = 1;

}

}
if (yearNumber == yyyy) 
{

n = dayOfYearNumber + (7 - weekDay) + (jan1WeekDay - 1);

weekNumber = n / 7;
if (jan1WeekDay > 4) weekNumber -= 1; 
}
return (weekNumber); 
}
public static bool IsLeapYear(int yyyy) 
{
return ((yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0)); 
} 

Remarque :
Repris de http://www.thecodeproject.com/csharp/GregToISO.asp