简体   繁体   中英

How to loop through a date range in which dates are stored as Integers?

I have to develop a system, where user will specify a starting range and an ending range (By Range, I mean to say a particular period, where PERIOD is given as a concatenation of YEAR AND MONTH ). For example, PERIOD = 201304 , where 2013 is the user entered Year and 04 is the MONTH. The user can specify a maximum range of upto 2 years only.

Data needs to be selected on the basis of the user entered range. The problem is whenever I try to loop through the period, the PERIOD changes after 201312 to 201313. I have separate variables for user selected year and month (start_year, start_month, end_year, end_month)

I did a IF loop there in which I tried to do the following

  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

I even tried thsi one :

IF (p_fromperiod MOD p_yearfrm = 13) THEN

    LET p_yearfrm = p_yearfrm + 1

    LET p_monthfrm = 01

    LET p_fromperiod = p_yearfrm + 1,p_monthfrm

Still the period changes after reaching 201212 to 201213. I want this to be 201301. Please help.

As you have probably guessed, you have some flaws in your logic. The first example is too hard to decipher, whilst the second shows you don't understand what the MOD operator does. If used correctly, in this context, it would be of the form IF variable MOD 12 = 0 ie every 12th value do something.

I think where you are going wrong is trying to do it all in one loop. I would keep it simple and break the problem into two nested loops, one for year, one for month

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

I would also encourage you to also post questions like this in the dedicated 4Js Genero developers forum at the 4Js website http://www.4js.com/fjs_forum/ as well as here. Most Genero developers should be aware of that forum and have access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM