Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO DECREMENT DATE???

Status
Not open for further replies.

IncredibleVolk

Technical User
Apr 2, 2004
67
0
0
US
I stoled some of this from other scripts on this site. This starts off giving me the first and last day of the month for the previous month. I was wondering if anyone knows how I loop this so it would decrement the month each time through.

January -08
December - 07
November - 07
etc...


proc Main
string sDate,sDate2
integer iYear,iMonth,iDay,iDay2,iHour,iMin,iSec
integer Loops
integer iWeekday,iDayYear,iLeapYear
long Timeval


txflush
pause 1
rxflush
pause 1

for Loops = 0 upto 1

**FIRST DAY OF THE MONTH**

ltimeints $LTIME iYear iMonth iDay iHour iMin iSec
if iMonth == 1 ;If currently January
iMonth = 12 ;Then assign month to December
iYear-- ;And decrement year by one
else
iMonth-- ;Otherwise just move month value back by one
endif
iDay = 1 ;First day of the month
strfmt sDate "%02d-%02d-%d" iMonth iDay iYear
strdelete sDate 6 2
usermsg "FIRST DATE %s" sDate

******LAST DAY OF THE MONTH**

if iDay2 <= 31
switch iMonth
case 1 ;January
case 3 ;March
case 5 ;May
case 7 ;July
case 8 ;August
case 10 ;October
case 12 ;December
iDay2 = 31 ;31 Days
endcase
case 4 ;April

case 5 ;May
case 6 ;June
case 9 ;September
case 11 ;November
iDay2 = 30 ;30 days
endcase
case 2 ;February
intsltime iYear iMonth iDay2 iHour iMin iSec TimeVal ;Convert to a long time value
ltimemisc TimeVal iWeekday iDayYear iLeapYear ;Get leap year value
if iLeapYear == 1 ;If leap year
iDay2 = 29 ;Set day to 29
else
iDay2 = 28
endif
endcase

endswitch
endif

strfmt sDate2 "%02d-%02d-%d" iMonth iDay2 iYear
strdelete sDate2 6 2
usermsg "SECOND DATE %s" sDate2
statmsg "Month %d" Loops

loopfor
exitfor
endfor

endproc
 
Probably the easiest way to do so would be to wrap the code (whatever needs to be executed each loop) after the ltimeints into a loop and continue decrementing the month as the script currently does now.

 
This is what I would do:

Initialize your Integer LOOPS to 1 at the beginning.

Add another Integer called GIVEME (or whatever you want to name it) it will be Initialized at however many previous months you want.

Integer GIVEME = 12

Change your "For LOOPS = 0 upto 1"
to "For LOOPS = 0 upto GIVEME" <i>which will give you twelve previous months worth...</i>

Next, right after you start the "for loop"
Add the small statement:
iMonth-LOOPS

And you're done!

This will subtract from the iMonth the current value of LOOPS, at the start of the for loop the current value is 0, so the loop will run normally. But each Loop after that will be decrimented by one more than the previous.

Let us all know if it works for you so others can get the same help.

Thanks
 
Sorry, in my previous post, I mentioned "Initializing LOOPS to 1 at the beginning". There is no need for that. It would still work because the for LOOPS = 0 upto 12 would undo the initialization, but I just wanted to communicate that that step is unnecessary.

Cheers!

Brandon Kennard
aka HuntersDad
 
Thank You for your replies. I'm not sure decrementing by just the month will work. You also need to rollback by year. This gave me the first day of the month for the previous two months.

for Loops = 0 upto 1

******GO BACK ONE MONTH************
lCurTime = $LTIME
ltimeints lCurTime iYear iMonth iDay iHour iMin iSec
if iMonth == 1
iMonth = 12
iDay = 1
iYear--
else
iMonth--
iDay = 1
endif

*******GO BACK TWO MONTHS**********************
if Loops == 1
lCurTime = $LTIME
ltimeints lCurTime iYear iMonth iDay iHour iMin iSec
if iMonth == 1
iMonth = 11
iDay = 1
iYear--
else
iMonth--
iMonth--
iDay = 1
endif
endif
 
Actually IncredibleVolk...

Your original code in your original post DID compensate for the rollback of the year with the snippet
Code:
if iMonth == 1  ;If currently January
iMonth = 12 ;Then assign month to December
iYear-- ;And decrement year by one

So adding what I had posted should complete the code for you.

Good Luck and be sure to let us know what your final solution was!
 
Huntersdad,

Upon further thought you were actually correct. I just needed to assign the month and decrement the year once we reached December.

Integer TotalMonths = 11

For Loops = 1 upto TotalMonths
iDay = 1
iMonth--

if iMonth == 0
iDay = 1
iMonth = 12
iYear--
endif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top