ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

calculation the correct number of the week

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • calculation the correct number of the week

    Hello!
    Does anybody know how to calculate the correct number of the week for a give date? I've tried some code, that was ready for download in the internet, but all calculated a wrong number of the week for December, 31st 2007. They all showed week number 53. The ISO standard tells that the number of the week for this given date must be 1.
    Every help is welcome
    Bernie

  • #2
    Re: calculation the correct number of the week

    found this on web didnt test

    heres a nice thread



    Code:
         H NoMain
    
          * ------------------------------------------------------------- Prototypes
         D DayOfWeek       PR             5I 0
         D                                 D   Value
    
         D WeekOfYear      PR             5I 0
         D                                 D   Value
    
          * ------------------------------------------------------------------------
          *
          * Procedure: DayOfWeek
          * Description: Retrieve day of week using ISO 8601 standard
          *              (0=Monday … 6=Sunday)
          *
         P DayOfWeek       B                   Export
    
         D DayOfWeek       PI             5I 0
         D  DateIn                         D   Value
    
         D NbrDays         S             10I 0
    
         D Monday          C                   D('2001-01-04')
    
          /Free
    
           NbrDays = %DIFF(DateIn:Monday:*DAYS);
           Return  = %REM( %REM(NbrDays:7) + 7 : 7);
    
          /End-Free
    
         P DayOfWeek       E
    
          * ------------------------------------------------------------------------
          *
          * Procedure: WeekOfYear
          * Description: Retrieve week of year using ISO 8601 standard
          *              (Year starts on Monday of week containing January 4)
          *
    
         P WeekOfYear      B                   Export
    
         D WeekOfYear      PI             5I 0
         D  DateIn                         D   Value
    
         D                 DS
         D Jan04Date                       D   INZ(D'0001-01-04')
         D  Jan04Year                     4  0 Overlay(Jan04)
    
         D FirstMonday     S               D
         D Jan04DOW        S              5I 0
    
          /Free
    
           // Change Jan04Date to target year,
           // then calculate first Monday of target year
           Jan04Year   = %SUBDT(DateIn:*Y);
           Jan04DOW    = DayOfWeek(Jan04Date);
           FirstMonday = Jan04Date - %DAYS(Jan04DOW);
    
           // If target date is before first Monday, switch to prior year
           If DateIn < FirstMonday;
             Jan04Year   = Jan04Year - 1;
             Jan04DOW    = DayOfWeek(Jan04Date);
             FirstMonday = Jan04Date - %DAYS(Jan04DOW);
           Endif;
    
           // Return week number (number of full weeks since first Monday + 1)
           Return %DIV(%DIFF(DateIn:FirstMonday:*DAYS):7) + 1;
    
          /End-Free
    
         P WeekOfYear      E
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Re: calculation the correct number of the week

      Exec sql Set :MyWeek = week_iso(date('2007-12-31'))
      End-exec

      returns 1.
      Philippe

      Comment

      Working...
      X