[英]How to loop through a date range in which dates are stored as Integers?
我必須開發一個系統,其中用戶將指定一個起始范圍和一個結束范圍(按范圍,我的意思是說一個特定的時期,其中PERIOD是YEAR AND MONTH的串聯 )。 例如, PERIOD = 201304 ,其中2013是用戶輸入的Year,而04是MONTH。 用戶只能指定最長2年的最大范圍。
需要根據用戶輸入的范圍選擇數據。 問題是,每當我嘗試遍歷該期間時,PERIOD就會在201312年到201313年之間發生變化。我為用戶選擇的年份和月份(start_year,start_month,end_year,end_month)分別設置了變量。
我在那里做了一個IF循環,在其中嘗試執行以下操作
FOR p_tmpyear = p_tempfrom TO p_tempto
IF (p_monthfrm < 12) THEN
LET p_yearfrm = p_yearfrm + 1
LET p_monthfrm = 01
LET p_fromperiod = p_yearfrm + 1,p_monthfrm >p_fromperiod is an integer storing concatenated Month and Year, to achieve the desired PERIOD format as mentioned above.
LET p_tempfrom = p_fromperiod
END IF
DISPLAY p_tmpyear
END FOR
我什至嘗試了一個:
IF(p_fromperiod MOD p_yearfrm = 13)然后
LET p_yearfrm = p_yearfrm + 1
LET p_monthfrm = 01
LET p_fromperiod = p_yearfrm + 1,p_monthfrm
在到達201212到201213之后,時間段仍會更改。我希望它是201301。請幫忙。
您可能已經猜到了,您的邏輯存在一些缺陷。 第一個示例太難破解,而第二個示例則表明您不了解MOD運算符的作用。 如果在此情況下正確使用,它將是IF變量MOD 12 = 0的形式,即,每12個值都會執行某項操作。
我認為您要出問題的地方是嘗試在一個循環中完成所有操作。 我會保持簡單,然后將問題分解為兩個嵌套循環,一個循環為一年,一個循環為月
DEFINE start_year, end_year, start_month, end_month INTEGER -- sample values 2012, 2013,7,6
DEFINE loop_year, loop_month INTEGER -- values used to loop through year and month respectively
DEFINE loop_first_month, loop_last_month INTEGER -- for each year, calc first month and last month
DEFINE period CHAR(6)
-- First loop over year
FOR loop_year = start_year TO end_year
-- Calculate first and last month for the given year
-- If first year, use passed in start month else use 1
IF loop_year = start_year THEN
LET loop_first_month = start_month
ELSE
LET loop_first_month = 1
END IF
-- If last year, use passed in end month else use 12
IF loop_year = end_year THEN
LET loop_last_month = end_month
ELSE
LET loop_last_month = 12
END IF
-- Second loop over month
FOR loop_month = loop_first_month TO loop_last_month
LET period = loop_year USING "&&&&",loop_month USING "&&"
DISPLAY period
END FOR
END FOR
我也鼓勵您在4Js網站http://www.4js.com/fjs_forum/以及此處的4Js Genero開發人員專用論壇中發布類似的問題。 大多數Genero開發人員應注意該論壇並具有訪問權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.