Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


How to find the current week of the year

Keywords:    week of year

Question:

I'd like to find out the current week of the year, so that I can put the output (example:40) into a variable and use it for variable path entries or changing directories.

Answer:

Convert your ymd to julien date format and divide by 7, with some rounding logic.

Something like (with added stuff here):

ToDate=TimeYmdHms()
ThisYear=ItemExtract(1,ToDate,":")
JanFirst="%thisyear%:01:01:00:00:00"
JanFirstJulian=TimeJulianDay(JanFirst)
ToJulian=TimeJulianDay(ToDate) 
dayofyear=ToJulian-JanFirstJulian+1
dayofweekcode=(ToJulian+5) mod 7
day=ItemExtract(dayofweekcode+1, "Sun Mon Tue Wed Thu Fri Sat", " ")

weekofyear=dayofyear/7

Message("WeekOfYear",weekofyear)

line=StrCat("Julian Date-> ", ToJulian,@CRLF,"Day of
Year->",DayofYear,@CRLF,"Day of week-> ",day)
Message(TimeDate(), line)

MORE....

;follows the ISO8601 standard for determining week numbers. 
;ISO8601 states that each week begins on a Monday, 
;and week #1 of any year is the first week that contains January 4. 
;That means that in some cases January 1, 2 and 3 could fall into 
;week #52 or #53 of the previous year. 
;The last few days of December could fall into week #1 
;of the following year. 

ToDate = TimeYmdHms()
;ToDate = askline('date','date:',ToDate)
ThisYear = ItemExtract(1,ToDate,":")
ToJulian = TimeJulianDay(ToDate) 

ThisYear = ThisYear + 2
while 1
   ThisYear = ThisYear - 1
	JanFirstJulian = TimeJulianDay("%thisyear%:01:01:00:00:00")
	DayJanFirst = ((JanFirstJulian+4) mod 7) +1
	OffSet = JanFirstJulian - DayJanFirst + int((DayJanFirst - 1)/4)*7
	WeekNumber = int((ToJulian - OffSet +6)/7)
	if WeekNumber > 0 then break
endwhile

Message('Year %ThisYear%: Weeknumber',WeekNumber) 

Article ID:   W13862
Filename:   Current Week of the Year.txt