IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

Calcul du numéro de la semaine


Sujet :

ASP.NET

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 16
    Points : 8
    Points
    8
    Par défaut Calcul du numéro de la semaine
    Bonjour,

    est ce que quelqu'un connaitrait un script pour calculer le numéro d'une semaine donnée en parametre (sans les bugs!) ??

  2. #2
    Membre éprouvé Avatar de guitoux1
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    1 011
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Juin 2006
    Messages : 1 011
    Points : 1 256
    Points
    1 256
    Par défaut
    Tu peux trouver ça avec la méthode GetWeekOfYear() de l'objet Calendar :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    System.Globalization.GregorianCalendar cal = new System.Globalization.GregorianCalendar(System.Globalization.GregorianCalendarTypes.Localized);
    int week = cal.GetWeekOfYear(DateTime.Now, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
    A utiliser avec le calendrier qui va bien (ici grégorien) et selon la culture. Par contre je suis pas certian du second paramètre (CalendarWeekRule.FirstDay)

  3. #3
    Membre éprouvé Avatar de guitoux1
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    1 011
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Juin 2006
    Messages : 1 011
    Points : 1 256
    Points
    1 256
    Par défaut
    Selon Wikipédia (http://fr.wikipedia.org/wiki/ISO_860...9ro_de_semaine), la semaine 1 est celle qui contient le premier jeudi de l'année. Autrement dit, la semaine qui possède au moins 4 jours dans la nouvelle année. Du coup, le bon code est :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    System.Globalization.GregorianCalendar cal = new System.Globalization.GregorianCalendar(System.Globalization.GregorianCalendarTypes.Localized);
    int week = cal.GetWeekOfYear(DateTime.Now, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 16
    Points : 8
    Points
    8
    Par défaut
    J'ai trouvé ca qui marche bien et sans bug du 1er janvier et du 31 decembre

    private static int WeekNumber_Entire4DayWeekRule(DateTime date)
    {
    // Updated 2004.09.27. Cleaned the code and fixed a bug.
    // Compared the algorithm with code published here .
    // Tested code successfully against the other algorithm
    // for all dates in all years between 1900 and 2100.
    // Thanks to Marcus Dahlberg for pointing out the deficient logic.

    // Calculates the ISO 8601 Week Number
    // In this scenario the first day of the week is monday,
    // and the week rule states that:
    // [...] the first calendar week of a year is the one
    // that includes the first Thursday of that year and
    // [...] the last calendar week of a calendar year is
    // the week immediately preceding the first
    // calendar week of the next year.
    // The first week of the year may thus start in the
    // preceding year

    const int JAN = 1;
    const int DEC = 12;
    const int LASTDAYOFDEC = 31;
    const int FIRSTDAYOFJAN = 1;
    const int THURSDAY = 4;
    bool ThursdayFlag = false;

    // Get the day number since the beginning of the year
    int DayOfYear = date.DayOfYear;

    // Get the numeric weekday of the first day of the
    // year (using sunday as FirstDay)
    int StartWeekDayOfYear =
    (int)(new DateTime(date.Year, JAN, FIRSTDAYOFJAN)).DayOfWeek;
    int EndWeekDayOfYear =
    (int)(new DateTime(date.Year, DEC, LASTDAYOFDEC)).DayOfWeek;

    // Compensate for the fact that we are using monday
    // as the first day of the week
    if (StartWeekDayOfYear == 0)
    StartWeekDayOfYear = 7;
    if (EndWeekDayOfYear == 0)
    EndWeekDayOfYear = 7;

    // Calculate the number of days in the first and last week
    int DaysInFirstWeek = 8 - (StartWeekDayOfYear);
    int DaysInLastWeek = 8 - (EndWeekDayOfYear);

    // If the year either starts or ends on a thursday
    // it will have a 53rd week
    if (StartWeekDayOfYear == THURSDAY || EndWeekDayOfYear ==
    THURSDAY)
    ThursdayFlag = true;

    // We begin by calculating the number of FULL weeks
    // between the start of the year and our date.
    // The number is rounded up, so the smallest possible value is 0.
    int FullWeeks = (int)Math.Ceiling((DayOfYear - (DaysInFirstWeek)) /
    7.0);

    int WeekNumber = FullWeeks;

    // If the first week of the year has at least four days, then
    // the actual week number for our date can be incremented by one.
    if (DaysInFirstWeek >= THURSDAY)
    WeekNumber = WeekNumber + 1;

    // If week number is larger than week 52
    // (and the year doesn't either start or end on a thursday)
    // then the correct week number is 1.
    if (WeekNumber > 52 && !ThursdayFlag)
    WeekNumber = 1;

    // If week number is still 0, it means that we are trying
    // to evaluate the week number for a week that belongs
    // in the previous year(since that week has 3 days or
    // less in our date's year).
    // We therefore make a recursive call using
    // the last day of the previous year.
    if (WeekNumber == 0)
    WeekNumber = WeekNumber_Entire4DayWeekRule(
    new DateTime(date.Year - 1, DEC, LASTDAYOFDEC));
    return WeekNumber;
    }

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 16
    Points : 8
    Points
    8
    Par défaut
    Merci en tout cas pour ton aide

  6. #6
    Membre éprouvé Avatar de guitoux1
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    1 011
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Juin 2006
    Messages : 1 011
    Points : 1 256
    Points
    1 256
    Par défaut
    OK. Juste que c'est la même chose, mais en 83 lignes de code au lieu de 2.
    Bonne chance

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XI] Calculer le numéro d'une semaine à partir d'une date
    Par leloup84 dans le forum SAP Crystal Reports
    Réponses: 2
    Dernier message: 23/04/2007, 14h49
  2. [EXP]Calcul du numéro de semaine
    Par aouregan dans le forum VBA Access
    Réponses: 3
    Dernier message: 23/03/2007, 15h55
  3. Calculer le numéro de semaine d'une date
    Par chandlerbing77 dans le forum Access
    Réponses: 2
    Dernier message: 09/06/2006, 12h26
  4. [excel]calcul du numéro de semaine
    Par Mugette dans le forum Macros et VBA Excel
    Réponses: 9
    Dernier message: 13/10/2005, 11h57
  5. Calcul des numéros de semaine d'un calendrier
    Par Invité dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 06/11/2002, 21h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo