Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Determine date based upon week number

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

    Determine date based upon week number

    Anyone have a method that will return the first or last day (date) of the week when only the week number is known?

    #2
    Re: Determine date based upon week number

    Connie,
    Not saying that I can do this off the top of my head but think it could be done with some manipulation of the current functions Alpha has....but regardless there HAS to be some reference point or it is impossible--given a week number who is to say it is for this year, last year, 20 years ago???? And then to have a function span more than one year could be even trickier.
    Mike
    __________________________________________
    It is only when we forget all our learning that we begin to know.
    It's not what you look at that matters, it's what you see.
    Henry David Thoreau
    __________________________________________



    Comment


      #3
      Re: Determine date based upon week number

      Very true, yes. In my instance I am working with dates in the future which helps to narrow it down. None would go into the future more than a year.

      Comment


        #4
        Re: Determine date based upon week number

        You could try a function to get a default date that you could use with a calendar control.

        Code:
        FUNCTION find_date AS D (week_no AS N)
        	dim nw as n
        	dim nd as d
        	dim vcStart as c
        	dim start as d
        	vcstart = "01/01/"+cyear(date())
        	start = ctod(vcStart)
        	nw = week_no * 7
        	nd = start + nw
        	find_date = nd
        END FUNCTION
        ?find_date(25)
        ={06/24/2008}

        Which is the 3rd day of the week - Tuesday.

        Your date field could have a field rule for the default value that uses the field that has the week_no value. Which I did not test yet.

        find_date(week_no)

        If you want to go past the current year, you could add another parameter to the function to set the start date to the first day of the year you want, which the user would have to input.

        Code:
        FUNCTION find_year AS D (week_no AS N,start as d)
        	dim nw as n
        	dim nd as d
        	nw = week_no * 7
        	nd = start + nw
        	find_year = nd
        END FUNCTION
        ?find_year(25,{01/01/2009})
        ={06/25/2009}
        Last edited by MoGrace; 04-17-2008, 05:25 PM.
        Robin

        Discernment is not needed in things that differ, but in those things that appear to be the same. - Miles Sanford

        Comment


          #5
          Re: Determine date based upon week number

          Code:
          dim a as n=16
          dim b as n=2008
          [B]?date_value(b,1,1)-dow(date_value(b,1,1))+1+(a-1)*7[/B]
          = {04/13/2008}
          [COLOR="RoyalBlue"]'The Sunday (first day) of week 16 of the year 2008[/COLOR]
          [B]?date_value(b,1,1)-dow(date_value(b,1,1))+7+(a-1)*7[/B]
          = {04/19/2008}
          [COLOR="RoyalBlue"]'The Saturday (last day) of week 16 of the year 2008[/COLOR]
          
          a=9
          b=2009
          [B]?date_value(b,1,1)-dow(date_value(b,1,1))+1+(a-1)*7[/B]
          = {02/22/2009}
          [COLOR="RoyalBlue"]'The Sunday of week 9 of the year 2009[/COLOR]
          
          [B]?date_value(b,1,1)-dow(date_value(b,1,1))+7+(a-1)*7[/B]
          = {02/28/2009}
          [COLOR="RoyalBlue"]'The Saturday of week 9 of the year 2009[/COLOR]
          
          You could make a function out of this.

          Comment


            #6
            Re: Determine date based upon week number

            A different take on the function.

            Code:
            FUNCTION wkfirst AS D (wk_num AS N, yr_num AS N )
            	if (.not. between(wk_num,1,53))
            		ui_msg_box("Sorry","Invalid week number.")
            	end if
            	select
            		case week(ctod("01/01/"+alltrim(str(yr_num)))) = week(ctod("01/07/"+alltrim(str(yr_num))))
            			'first day of year is a Sunday
            			wkfirst = ctod("01/01/"+alltrim(str(yr_num)))+ ((wk_num-1)*7)
            			
            		case week(ctod("01/01/"+alltrim(str(yr_num)))) = week(ctod("01/06/"+alltrim(str(yr_num))))
            			'first day of year is a Monday
            			wkfirst = ctod("01/01/"+alltrim(str(yr_num)))+ ((wk_num-1)*7)-1
            			if year(wkfirst)< yr_num
            				wkfirst = wkfirst +1
            			end if
            		
            		case week(ctod("01/01/"+alltrim(str(yr_num)))) = week(ctod("01/05/"+alltrim(str(yr_num))))
            			'first day of year is a Tuesday
            			wkfirst = ctod("01/01/"+alltrim(str(yr_num)))+ ((wk_num-1)*7)-2
            			if year(wkfirst)< yr_num
            				wkfirst = wkfirst +2
            			end if
            			
            		case week(ctod("01/01/"+alltrim(str(yr_num)))) = week(ctod("01/04/"+alltrim(str(yr_num))))
            			'first day of year is a Wednesday
            			wkfirst = ctod("01/01/"+alltrim(str(yr_num)))+ ((wk_num-1)*7)-3
            			if year(wkfirst)< yr_num
            				wkfirst = wkfirst +3
            			end if
            			
            		case week(ctod("01/01/"+alltrim(str(yr_num)))) = week(ctod("01/03/"+alltrim(str(yr_num))))
            			'first day of year is a Thursday
            			wkfirst = ctod("01/01/"+alltrim(str(yr_num)))+ ((wk_num-1)*7)-4
            			if year(wkfirst)< yr_num
            				wkfirst = wkfirst +4
            			end if
            			
            		case week(ctod("01/01/"+alltrim(str(yr_num)))) = week(ctod("01/02/"+alltrim(str(yr_num))))
            			'first day of year is a Friday
            			wkfirst = ctod("01/01/"+alltrim(str(yr_num)))+ ((wk_num-1)*7)-5
            			if year(wkfirst)< yr_num
            				wkfirst = wkfirst +5
            			end if
            			
            		case else
            			'first day of year is a Saturday
            			wkfirst = ctod("01/01/"+alltrim(str(yr_num)))+ ((wk_num-1)*7)-6
            			if year(wkfirst)< yr_num
            				wkfirst = wkfirst +6
            			end if
            			
            	end select
            			
            END FUNCTION
            There can be only one.

            Comment


              #7
              Re: Determine date based upon week number

              Here it is in a function.

              This function determines what date is the Sunday of a certain week #:

              Code:
              FUNCTION week_Sun AS D (Week_No AS N, Year AS N )	
              week_Sun=date_value(year,1,1)-dow(date_value(year,1,1))+1+(week_no-1)*7
              END FUNCTION
              ?week_sun(9,2009)
              = {02/22/2009}

              You could make one for Monday, Tuesday..etc.
              Code:
              FUNCTION week_Mon AS D (Week_No AS N, Year AS N )
              week_Mon=date_value(year,1,1)-dow(date_value(year,1,1))+2+(week_no-1)*7
              END FUNCTION
              ?week_mon(9,2009)
              = {02/23/2009}
              Last edited by G Gabriel; 04-17-2008, 06:51 PM.

              Comment


                #8
                Re: Determine date based upon week number

                And if you want just one function that does it all:
                Code:
                FUNCTION Date_From_Week_NO AS D (Week_day as C,Week_No AS N, Year AS N )
                x=case(Week_day="sun",1,Week_day="mon",2,Week_day="tue",3,Week_day="wed",
                4,Week_day="thu",5,Week_day="fri",6,.t.,7)
                Date_From_Week_NO=date_value(year,1,1)-dow(date_value(year,1,1))+x+(week_no-1)*7
                END FUNCTION
                ?date_from_week_no("sun",9,2009)
                = {02/22/2009}

                ?date_from_week_no("mon",9,2009)
                = {02/23/2009}

                ?date_from_week_no("tue",9,2009)
                = {02/24/2009}

                ...and so on..

                Comment


                  #9
                  Re: Determine date based upon week number

                  ..and..
                  further simplified:
                  Code:
                  FUNCTION Date_From_Week_NO AS D (Week_day as C,Week_No AS N, Year AS N )
                  Date_From_Week_NO=date_value(year,1,1)-dow(date_value(year,1,1))+
                  day_number(Week_day)+(week_no-1)*7
                  END FUNCTION

                  Comment


                    #10
                    Re: Determine date based upon week number

                    Originally posted by G Gabriel View Post
                    ..and..
                    further simplified:
                    Code:
                    FUNCTION Date_From_Week_NO AS D (Week_day as C,Week_No AS N, Year AS N )
                    Date_From_Week_NO=date_value(year,1,1)-dow(date_value(year,1,1))+
                    day_number(Week_day)+(week_no-1)*7
                    END FUNCTION
                    That's great Gabe, I hope Connie sees it.
                    Robin

                    Discernment is not needed in things that differ, but in those things that appear to be the same. - Miles Sanford

                    Comment


                      #11
                      Re: Determine date based upon week number

                      Wow! Amazing how quickly you can come up with the solution. Thanks!

                      Comment


                        #12
                        Re: Determine date based upon week number

                        The answers so far may have problems with the first and last weeks of a year, and don't forget some years have 54 weeks.

                        To stay within a given week number you may want to try the following, which work for future week numbers including the year 2028 (the next 54 week year). You never know, you may be using your program then even though it will be outdated. I am still using a DOS based FoxBase program I wrote in the 1980's, but desperately trying to learn Alpha5 to get out of it.



                        Code:
                        FUNCTION weekstart AS D (wk AS N, yr AS N )
                        	'Finds the first day of a given week and year
                        	if wk > 53 then
                        		wk = 53
                        		if yr = 2028 then
                        		    wk = 54
                        		end if
                        	end if		
                        	if wk < 2 then
                        	    weekstart = date_value(yr,1,1)
                        	else
                        	    weekstart = date_value(yr,1,1)+8-dow(date_value(yr,1,1))+7*(wk-2)
                        	end if
                        END FUNCTION




                        Code:
                        FUNCTION weekend AS D (wk AS N, yr AS N )
                        	'Finds the last day of a given week and year
                        	if wk < 2 then
                        	    weekend = date_value(yr,1,1)+7-dow(date_value(yr,1,1))
                        	else
                        	    weekend = date_value(yr,1,1)+8-dow(date_value(yr,1,1))+7*(wk-2)+6
                        	    if year(weekend) > yr then
                        	        weekend = date_value(yr,12,31)
                        	    end if
                        	end if
                        END FUNCTION
                        Alpha Five v12.3, Build 2999

                        Comment


                          #13
                          Re: Determine date based upon week number

                          What problem are you refering to?
                          Code:
                          ?Date_From_Week_NO("sun",1,2008)
                          = {12/30/2007}
                          
                          ?Date_From_Week_NO("sun",1,2028)
                          = {12/26/2027}
                          
                          ?Date_From_Week_NO("sun",53,2028)
                          = {12/24/2028}
                          
                          ?Date_From_Week_NO("sun",54,2028)
                          = {12/31/2028}
                          The original question states that the number of the week is" known".

                          Comment


                            #14
                            Re: Determine date based upon week number

                            If you want yet a more comerhensive function to accomodate for the possibility that the week number may or may not be correct, that is not hard, but also not needed as alpha arleady has a built-in function to tell you the week number:
                            week().

                            So, if you use that function along with this UDF, you are all set. You could, if you want, incorporate the week() function in the UDF, as in:

                            Code:
                            FUNCTION Date_From_Week_NO AS D (Week_day as C,Date AS D, Year AS N )
                            Date_From_Week_NO=date_value(year,1,1)-dow(date_value(year,1,1))+
                            day_number(Week_day)+(week(date)-1)*7
                            END FUNCTION
                            and that should do it for you.
                            Last edited by G Gabriel; 04-18-2008, 05:36 PM.

                            Comment


                              #15
                              Re: Determine date based upon week number

                              I even simplified it a little more and now all you need is two parameters only, the weekday and the date in question:
                              Code:
                              FUNCTION Date_From_Week_NO1 AS D (Week_day as C,Date AS D)
                              Date_From_Week_NO1=date_value(year(date),1,1)-dow(date_value(year(date),
                              1,1))+day_number(Week(date))+(week(date)-1)*7
                              END FUNCTION
                              ?Date_From_Week_NO1("sun",{12/31/2028})
                              = {12/31/2028}

                              ?Date_From_Week_NO1("sun",{12/29/2028})
                              = {12/24/2028}

                              So, now you got 2 functions, take your pick.
                              Last edited by G Gabriel; 04-18-2008, 05:36 PM.

                              Comment

                              Working...
                              X