Lag methodology

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Lag methodology

#1 Post by SIMMS7400 » 14 Mar 2019 05:14

Hi Folks -

I have a unique need to implement a lag type of methodology.

For instance, I have a date variable set as such:

Code: Select all

SET "DATEVAR=31-Jan-18"
I also need to set (4) additional variable that lag 4 days. For instance:

Code: Select all

SET "DATEVAR1=30-Jan-18"
SET "DATEVAR2=29-Jan-18"
SET "DATEVAR3=28-Jan-18"
SET "DATEVAR4=27-Jan-18"
What is the best and most dynamic way to approach this? I need this to be able to cross over years as well. Thank you!

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Lag methodology

#2 Post by jeb » 14 Mar 2019 05:22

Hi SIMMS7400,

I would convert the date to julian date (number of days).
Then use some math to add/subtract days and convert it back from the julian format to gregorian date.

Look here
:date2jdate - converts a gregorian calender date to julian day format
:jdate2date - converts julian days to gregorian date format

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

Re: Lag methodology

#3 Post by Squashman » 14 Mar 2019 21:25

Lots of examples of Date math on the forum. PowerShell is another good option because you can easily set the output format. You can even use XCOPY to help with date math.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Lag methodology

#4 Post by SIMMS7400 » 18 Mar 2019 18:16

Hi All -

Thank for the links! I'm been playing around with that but can't seem to get anything worth that would be dynamic enough. Was hoping this was a quick hit. Does anyone mind sharing an example I could extrapolate on?

Gracias!

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

Re: Lag methodology

#5 Post by Squashman » 18 Mar 2019 20:26

Come on. You have been a member long enough to make an attempt!

viewtopic.php?t=5051

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

Re: Lag methodology

#6 Post by Aacini » 19 Mar 2019 16:24

I would like to present a simpler approach:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set /P "YYYYMMDD=Enter date in YYYYMMDD format: "
set /P "days=Enter number of days: "

set /A "DD=100+(D=YYYYMMDD%%100), YYYYMM=YYYYMMDD/100, MM=100+(M=YYYYMM%%100), YYYY=YYYYMM/100

for /L %%i in (1,1,%days%) do (
   echo !YYYY!-!MM:~1!-!DD:~1!
   set /A "C1=^!(D-=1),M-=C1*(1-12*(C2=^!(M-1))),YYYY-=C1*C2,MM=100+M,DD=100+(D+=C1*(30+((M+(M>>3))&1)-^!(M-2)*(2-^!(YYYY%%4))))"
)
Antonio

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Lag methodology

#7 Post by SIMMS7400 » 01 Apr 2019 18:46

Aacini wrote:
19 Mar 2019 16:24
I would like to present a simpler approach:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set /P "YYYYMMDD=Enter date in YYYYMMDD format: "
set /P "days=Enter number of days: "

set /A "DD=100+(D=YYYYMMDD%%100), YYYYMM=YYYYMMDD/100, MM=100+(M=YYYYMM%%100), YYYY=YYYYMM/100

for /L %%i in (1,1,%days%) do (
   echo !YYYY!-!MM:~1!-!DD:~1!
   set /A "C1=^!(D-=1),M-=C1*(1-12*(C2=^!(M-1))),YYYY-=C1*C2,MM=100+M,DD=100+(D+=C1*(30+((M+(M>>3))&1)-^!(M-2)*(2-^!(YYYY%%4))))"
)
Antonio
Wow Antonio!!! This is fantastic! I ended up making a batch/vb hybrid but I'm going to use this instead.

Thank you again!

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

Re: Lag methodology

#8 Post by Aacini » 02 Apr 2019 07:19


SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Lag methodology

#9 Post by SIMMS7400 » 03 Apr 2019 17:42

Hi Antonio -

I see the code you posted on the new thread is a bit different than the one above - which one should be used?

Thank you!

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

Re: Lag methodology

#10 Post by Aacini » 04 Apr 2019 14:26

That code allows you to generate a series of dates in both upward (future dates) and downward (past dates) from a base date.

Antonio

Post Reply