Page 1 of 1

Lag methodology

Posted: 14 Mar 2019 05:14
by SIMMS7400
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!

Re: Lag methodology

Posted: 14 Mar 2019 05:22
by jeb
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

Re: Lag methodology

Posted: 14 Mar 2019 21:25
by Squashman
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.

Re: Lag methodology

Posted: 18 Mar 2019 18:16
by SIMMS7400
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!

Re: Lag methodology

Posted: 18 Mar 2019 20:26
by Squashman
Come on. You have been a member long enough to make an attempt!

viewtopic.php?t=5051

Re: Lag methodology

Posted: 19 Mar 2019 16:24
by Aacini
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

Re: Lag methodology

Posted: 01 Apr 2019 18:46
by SIMMS7400
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!

Re: Lag methodology

Posted: 02 Apr 2019 07:19
by Aacini

Re: Lag methodology

Posted: 03 Apr 2019 17:42
by SIMMS7400
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!

Re: Lag methodology

Posted: 04 Apr 2019 14:26
by Aacini
That code allows you to generate a series of dates in both upward (future dates) and downward (past dates) from a base date.

Antonio