Page 1 of 1

Iterating For The Last 60 Days

Posted: 30 Oct 2015 11:19
by Samir
This is going to be a theoretical question to see some of the logic presented for such a solution. There's not a specific application (as of yet), but I've had this in mind for a while and was trying to think of the various way to approach it.

Say you have a date, 10/30/2015. The month, day, and year are stored as variables you can access. For example:

Code: Select all

SET MM=10
SET DD=30
SET YYYY=2015
You are using the date as a variable in order to do something, for example echo the date:

Code: Select all

ECHO MM/DD/YYYY
If you wanted to iterate this for the last 60 days, what would your strategy be? How would you deal with the end of the previous month? Invalid dates like 09/31/2015 don't matter if the code is easier to implement and read.

Ideas?

Re: Iterating For The Last 60 Days

Posted: 30 Oct 2015 11:44
by Squashman
I would just use Dave's GetTimeStamp.bat

Code: Select all

C:\BatchFiles\JGetTimeStamp>getTimeStamp -d '10-30-2015' -od -60 -f {yyyy}{mm}{dd}
20150831

Re: Iterating For The Last 60 Days

Posted: 30 Oct 2015 13:34
by Samir
Squashman wrote:I would just use Dave's GetTimeStamp.bat

Code: Select all

C:\BatchFiles\JGetTimeStamp>getTimeStamp -d '10-30-2015' -od -60 -f {yyyy}{mm}{dd}
20150831
But this is hybrid. I want to do it in pure batch.

The ideas I've come up with so far involves decrementing the date using set and then looping. But addressing the month if the starting date is 01 and also when to stop are the things that make this idea messy.

I know there's smarter ways to do this, but I'm not that smart. :(

Re: Iterating For The Last 60 Days

Posted: 30 Oct 2015 13:51
by penpen

Re: Iterating For The Last 60 Days

Posted: 30 Oct 2015 13:55
by Aacini
You must use the "Date to Julian Day Number" conversion to convert a date to a day number, that may be directly incremented, and the opposite conversion to convert back the number to date. You may use the conversion "functions" described at this post in order to do these conversions in a simpler way. For example:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

:: Define YYYYMMDD date to Julian Day Number conversion function and viceversa

set "DateToJDN(YMD)=( a=(YMD), y=a/10000, a%%=10000, m=a/100, d=a%%100, a=(m-14)/12, (1461*(y+4800+a))/4+(367*(m-2-12*a))/12-(3*((y+4900+a)/100))/4+d-32075 )"
set "JDNtoDate(JDN)=( a=(JDN), l=a+68569,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447,d=l-(2447*j)/80,l=j/11,m=j+2-(12*l),y=100*(n-49)+i+l,y*10000+m*100+d )"

SET MM=10
SET DD=30
SET YYYY=2015

SET DAYS=60

rem Convert the date to Julian Day Number
set /A "number=%DateToJDN(YMD):YMD=!YYYY!!MM!!DD!% - DAYS"

rem Increment the number and convert it back to date
for /L %%i in (1,1,%DAYS%) do (
   set /A "YYYYMMDD=%JDNtoDate(JDN):JDN=number%, number+=1"
   echo !YYYYMMDD:~4,2!/!YYYYMMDD:~6,2!/!YYYYMMDD:~0,4!
)

Antonio

Re: Iterating For The Last 60 Days

Posted: 30 Oct 2015 13:56
by Squashman

Bingo. Was looking for that thread. My Google Foo is off today!

Re: Iterating For The Last 60 Days

Posted: 30 Oct 2015 17:27
by Samir
Thank you so much gentleman! I didn't think about converting the date and then converting it back. Let me think on this idea. 8)

Re: Iterating For The Last 60 Days

Posted: 17 Nov 2015 14:26
by Sponge Belly
Hi Samir! :)

Perhaps this post from Nov 2013 may be of interest. ;)

- SB

Re: Iterating For The Last 60 Days

Posted: 17 Nov 2015 15:23
by Samir
Sponge Belly wrote:Hi Samir! :)

Perhaps this post from Nov 2013 may be of interest. ;)

- SB
Thank you! I forgot about this thread. Good reading. 8)