Iterating For The Last 60 Days

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Samir
Posts: 360
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Iterating For The Last 60 Days

#1 Post by Samir » 30 Oct 2015 11:19

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?

Squashman
Expert
Posts: 4160
Joined: 23 Dec 2011 13:59

Re: Iterating For The Last 60 Days

#2 Post by Squashman » 30 Oct 2015 11:44

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

Samir
Posts: 360
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Iterating For The Last 60 Days

#3 Post by Samir » 30 Oct 2015 13:34

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. :(

penpen
Expert
Posts: 1820
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Iterating For The Last 60 Days

#4 Post by penpen » 30 Oct 2015 13:51


Aacini
Expert
Posts: 1646
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Iterating For The Last 60 Days

#5 Post by Aacini » 30 Oct 2015 13:55

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

Squashman
Expert
Posts: 4160
Joined: 23 Dec 2011 13:59

Re: Iterating For The Last 60 Days

#6 Post by Squashman » 30 Oct 2015 13:56


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

Samir
Posts: 360
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Iterating For The Last 60 Days

#7 Post by Samir » 30 Oct 2015 17:27

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)

Sponge Belly
Posts: 198
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Iterating For The Last 60 Days

#8 Post by Sponge Belly » 17 Nov 2015 14:26

Hi Samir! :)

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

- SB

Samir
Posts: 360
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Iterating For The Last 60 Days

#9 Post by Samir » 17 Nov 2015 15:23

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)

Post Reply