[SOLVED] Finding the week number for any given year.
I have many transactions with a date time value and I want to determine within what week of the year that date time value falls. For example there are 52 weeks in a year and week 1 is 1/1/08 to 1/7/08 and week 2 is 1/8/08 to 1/14/08 and week 52 is 12/25/08 to 12/31/08 etc... I will then graph the results for the year; thus, I will have 52 plots on a graph which represents all my data for the full year of transactions. thanks for your time!This message has been edited. Last edited by: Kerry,
December 04, 2008, 04:23 PM
Spence
DEFINE FILE CAR DATE1/A10 WITH COUNTRY= TODAY(DATE); DATE2/HMDYYs = HINPUT(14, DATE1, 8, 'HMDYYs'); WEEK/I2 = HPART(DATE2, 'WEEK', 'I2'); END -* TABLE FILE CAR SUM MAX.DATE1 MAX.DATE2 MAX.WEEK END
WF 8 version 8.2.04. Windows. In focus since 1990.
December 04, 2008, 05:04 PM
Francis Mariani
Unfortunately, this does not give the week number that Denny99 is looking for. The WEEK component of the Date-Time column is a Saturday-Friday week number (the range of which you can modify using the WEEKFIRST setting), while Denny99 is looking for a week number starting on the first day of the year, which may not be a Monday so there might be a discrepancy between the two.
2009:
m t w t f s s | m t w t f s s
-- -- -- -- -- -- -- | -- -- -- -- -- -- --
1 2 3 4 | 5 6 7 8 9 10 11
0 0 1 1 | 1 1 1 1 1 2 2 <== week component of date-time field
1 1 1 1 | 1 1 1 2 2 2 2 <== week number
I think you have to do this using the day-of-year component and divide by 7.
DEFINE FILE CAR
DATE1/A8 WITH COUNTRY= '20080108';
DATE2/HMDYYS = HINPUT(14, DATE1, 8, 'HMDYYS');
WEEK/I2 = HPART(DATE2, 'WEEK', 'I2');
DOY/I3 = HPART(DATE2, 'DAY-OF-YEAR', 'I3');
WEEKB/I2 = IF IMOD(DOY, 7, 'I3') EQ 0 THEN (DOY / 7) ELSE (DOY / 7) + 1;
END
TABLE FILE CAR
SUM
MAX.DATE1
MAX.DATE2
MAX.WEEK
MAX.DOY
MAX.WEEKB
END
Francis
Give me code, or give me retirement. In FOCUS since 1991
Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
December 04, 2008, 06:09 PM
Darin Lee
Probably doesn't matter much, but the last day of the year is going to give you a 53rd plot point because a year is 52 weeks and 1 (or 2) days. Francis' code, for most purposes, solves the problem as it uses modular division but that still doesn't resolve the issue of the extra day(s).
Regards,
Darin
In FOCUS since 1991 WF Server: 7.7.04 on Linux and Z/OS, ReportCaster, Self-Service, MRE, Java, Flex Data: DB2/UDB, Adabas, SQL Server Output: HTML,PDF,EXL2K/07, PS, AHTML, Flex WF Client: 77 on Linux w/Tomcat
December 04, 2008, 09:00 PM
Waz
As the length of a year is virtually constant, give or take 1 second from year to year, the calc of weeks 1 to 52/53 is the best way.
All depends on what Denny99 wants in his graph.
Waz...
Prod:
WebFOCUS 7.6.10/8.1.04
Upgrade:
WebFOCUS 8.2.07
OS:
Linux
Outputs:
HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!
December 05, 2008, 02:04 AM
Tony A
... and don't forget that there are differences between week numbers depending upon what standard you need to use - MS or ISO 8601.
ISO 8601 begins numbering weeks for the first week to contain a Thursday (other factors as well but this is the main one) whereas MS (as usual) decide that week number one is the first whole week within a year.
Find out which convention that you are required to use and code accordingly.
T
In FOCUS since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2
WebFOCUS App Studio 8.2.06 standalone on Windows 10
December 05, 2008, 09:13 AM
FrankDutch
Thinking of week numbers and things like that I would recommend this article.