Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#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:
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!
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#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
#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!
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#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
#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:
#8
Post
by Aacini » 02 Apr 2019 07:19
-
SIMMS7400
- Posts: 546
- Joined: 07 Jan 2016 07:47
#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:
#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