Purpose
Determine the week for a given date

For a given date you want to determine the week 1...53 and the year corresponding to this week. The date is specified in one of the external date formats (like dd.mm.yyyy or mm/dd/yyyy).

For example, 01.01.2003 is in week 1 of the year 2003. This sounds self-evident, but (surprisingly):

01.01.2005 is in week 53 of the year 2004
31.12.2003 is in week 1 of the year 2004

The rule is: a week is considered to be in the year that contains 4 days or more of the week. A week always starts with Monday.

Solution 1 (Function call)

// sample value
// any date in valid date format:
// d.m.y, m/d/y, m-d-y, y.m.d, y/m/d, y-m-d
Set V[mydate] "01.01.2021"

// use date calculations to force format YYYYMMDD
Set V[diff] "&V[mydate]" - "01.01.1900"
Set V[d] "01.01.1900" + "&V[diff]"
Set V[yyyymmdd] "&V[d](7-10)&V[d](4-5)&V[d](1-2)"

// Determine week
call "DATE_GET_WEEK" In.DATE="&V[yyyymmdd]" Out.WEEK="yearweek"

Set V[year] "&V[yearweek](1-4)"
Set V[week] "&V[yearweek](5-6)"

Message "&V[mydate] is in week &V[week] of the year &V[year]"
 

Sample message 1


 

Solution 2 (Date calculations)

// sample value
// any date in valid date format:
// d.m.y, m/d/y, m-d-y, y.m.d, y/m/d, y-m-d
Set V[mydate] "01.01.2021"

// Get date in format dd.mm.yyyy, to avoid handling all different date formats
Set V[reldate] "&V[mydate]" - "01.01.1000"
Set V[date] "01.01.1000" + "&V[reldate]"

Set V[year] "&V[date](7-10)"

label compute_week

// Get start of 1st week
Set V[firstweek] "01.01.&V[year]" / 7 decimals=0
Set V[firstweek] "&V[firstweek]" * 7

// Get week number
Set V[week] "&V[date]" * 1
Set V[week] "&V[week]" - "&V[firstweek]"
Set V[week] "&V[week]" + 4
Set V[week] "&V[week]" / 7 decimals=0

// Last week of previous year? (can be 52 or 53)
if V[week=0]
  Set V[year] "&V[year]" - 1
 
Set V[date] "31.12.&V[year]"
  goto compute_week
endif

// ... or first week of following year?
if V[week=53]
   Set V[lastweek] "&V[firstweek]" + 364
 
if V[lastweek>28.12.&V[year]]
      Set V[year] "&V[year]" + 1
 
  Set V[week] 1
 
endif
endif

Message "&V[mydate] is in week &V[week] of the year &V[year]"
 

Sample message 2


 

Components
InputAssistant