Announcement

Collapse
No announcement yet.

Calculating the Sunday from current date

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

  • Calculating the Sunday from current date

    Hi,

    I need a routine that will help me determine the last Sunday from the current date. For example todays date is 20100820. I need to figure out the date in which last Sunday was on. In this case Sunday = 20100815.

    Thanks,

    DAC

  • #2
    Re: Calculating the Sunday from current date

    PHP Code:
    Exec Sql Set :LastSunday =  current date dayofweek(current datedays
    Last edited by Mercury; August 20, 2010, 09:54 AM.
    Philippe

    Comment


    • #3
      Re: Calculating the Sunday from current date

      I think you need a " + 1 Day" at the end of that Select

      As in...

      Code:
      SELECT current date - dayofweek(current date) days + 1 Day
      FROM SYSIBM/SYSDUMMY1
      -Rick

      _____________________________

      Need a Managed File Transfer Solution -OR- iSeries Encryption?
      GoAnywhere and Crypto Complete by Linoma Software

      Comment


      • #4
        Re: Calculating the Sunday from current date

        I don't think so. Try it out without " + 1 day ".

        Edit :

        The first day of "my" week is Monday... So actually " + 1 day " is correct if the first day of the week is Sunday.
        Heck why do YOU start the weeks on Sunday ?
        Last edited by Mercury; August 20, 2010, 10:11 AM.
        Philippe

        Comment


        • #5
          Re: Calculating the Sunday from current date

          Hi dcutaia:

          I got this:
          Code:
          *// Procedure:  DayOfWeek                                           //
          *// Purpose:  Determine the day of week for a particular date       //
          *// Parameters:                                                     //
          *//    I: dt   -- date                                              //
          *// Returns:                                                        //
          *//    0..6    -- 0=Sunday, 1=Monday, 2=Tuesday, etc.               //
          *// Notes:                                                          //
          *//    January 5, 1800 is a Sunday.  This procedure only works for  //
          *//    dates later than 1800-01-05.                                 //
          *////////////////////////////////////
          P DayOfWeek       b
          D DayOfWeek       pi            10i 0
          D   dt                            d   value datfmt(*iso)
          /free
             return %rem (%diff (dt: d'1800-01-05': *days): 7);
          /end-free
          P DayOfWeek       e
          here:
          http://search400.techtarget.com/expe...2_mem1,00.html

          if you pass in today's date then subtract the returned number of days from today's date and that will be sunday

          Best of Luck
          GLS
          The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

          Comment


          • #6
            Re: Calculating the Sunday from current date

            Originally posted by Mercury View Post
            Heck why do YOU start the weeks on Sunday ?[/I]
            Because God rested from his work on the 7th day, which became the sabbath. The sabbath is a Saturday, or the 7th day, therefore the first day had to be Sunday. Even God knows that Monday is a bad day to start the week with.
            Michael Catalani
            IS Director, eCommerce & Web Development
            Acceptance Insurance Corporation
            www.AcceptanceInsurance.com
            www.ProvatoSys.com

            Comment


            • #7
              Re: Calculating the Sunday from current date

              Originally posted by Mercury
              Heck why do YOU start the weeks on Sunday ?[/I]
              Originally posted by MichaelCatalani
              Because God rested from his work on the 7th day, which became the sabbath. The sabbath is a Saturday, or the 7th day, therefore the first day had to be Sunday. Even God knows that Monday is a bad day to start the week with.
              Not only you rest on Saturdays but also on Sundays, therefore you start the week by a day off, and so rest before work. What a strange habit ! Starting the week on Monday is IMO a good starting point because it looks logical and reasonable since you do work the first 5 days of the week and rest after (and not before) on Saturday and Sunday the 7th day both days also called week-end not "week-start".
              Additionally see the ISO 8601 rules which precisely stipulate that the week should start on Monday.
              Last edited by Mercury; August 22, 2010, 03:59 AM. Reason: Add week-end days.
              Philippe

              Comment


              • #8
                Re: Calculating the Sunday from current date

                Sorry guys,

                but the SQL scalar function DayOfWeek() returns 1 for Sunday and 7 for Saturday, while the scalar function DayOfWeek_ISO() returns 1 for Monday and 7 for Sunday.
                To determine the date of the last sunday, you may use the following formula:
                Code:
                /Free
                   Set :LastSunday = Current_Date - DayOfWeek_ISO(Current_Date) Days;
                /End-Free
                If you are on release V5R4 or higher you also could use the following statement to determine the last sunday:
                Code:
                /Free
                   Set :LastSunday = Next_Day(Current_Date, 'SUN') - 7 Days;
                /End-If
                Birgitta

                Comment


                • #9
                  Re: Calculating the Sunday from current date

                  Originally posted by Mercury View Post
                  Not only you rest on Saturdays but also on Sundays, therefore you start the week by a day off, and so rest before work. What a strange habit ! Starting the week on Monday is IMO a good starting point because it looks logical and reasonable since you do work the first 5 days of the week and rest after (and not before) on Saturday and Sunday the 7th day both days also called week-end not "week-start".
                  Additionally see the ISO 8601 rules which precisely stipulate that the week should start on Monday.
                  I like your logic -- "week end" vs "week start."
                  http://www.linkedin.com/in/chippermiller

                  Comment


                  • #10
                    Re: Calculating the Sunday from current date

                    Originally posted by Mercury;54288Additionally see the [URL="http://en.wikipedia.org/wiki/ISO_8601"
                    ISO 8601 rules[/URL] which precisely stipulate that the week should start on Monday."
                    I guess they don't follow ISO 8601 over here then ...
                    Greg Craill: "Life's hard - Get a helmet !!"

                    Comment

                    Working...
                    X