Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How can I find the week beginning date (Sunday) for a given date?

How can I find the week beginning date (Sunday) for a given date?

The function below for a given date returns the date of Sunday for that week by:

  1. Finding the number of the day of the week using FormatDateTime( ) i.e. Sunday = 1, Monday = 2 etc
  2. Finding how many days it is away from Sunday i.e. Monday is 1 day from Sunday and Saturday is 6
  3. Modifying the date using AddDateTime( )

Example Testcase:

testcase Start_of_Week()  
       Print(FormatDateTime( printDT("2007-01-01") , "yyyy-mm-dd"))

Results:

2006-12-31

The Function

Datetime printDT(Datetime dt)
    DATETIME dtWeekStart
    STRING sDay
                INTEGER iOffset=0,iDayNumber
    //find out the day number ie 1 for Sunday, 2 for Saturday etc
    sDay=FormatDateTime( dt , "w")
    //convert the number to a integer
     iDayNumber = val(sDay)
    //find how many days away from Sunday it is   
    if iDayNumber==1
        iOffset=0
    if iDayNumber==2
        iOffset=-1
    if iDayNumber==3
        iOffset=-2
    if iDayNumber==4
        iOffset=-3
    if iDayNumber==5
        iOffset=-4
    if iDayNumber==6
        iOffset=-5
    if iDayNumber==7
        iOffset=-6
    //modify date so it is a sunday
    dtWeekStart = AddDateTime(dt,iOffset)
    return dtWeekStart

Old KB# 21189

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:17
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.