-1

I would like to get a list of date for every Monday in a year using Batch Script.

Sample input:

2018

Sample output:

2018-01-01
2018-01-08
2018-01-15
2018-01-22
2018-01-29
...
2018-12-03
2018-12-10
2018-12-17
2018-12-24
2018-12-31

I found the below post which return the date of last Sunday in every month: https://rosettacode.org/wiki/Find_the_last_Sunday_of_each_month#Batch_File

I tried to modified it, but it doesn't seem to work with my requirement.

I was also thinking of getting the first Monday date in a year and add 7 to that date with a loop until the end of the year. However, I am struggling to find an example to do so.

Any help is great appreciated.

Thanks,

Daniel

  • Please post the code you are using by [edit]ing the question! Read also this: [mcve]. Anyway, batch is not the best choice for date arithmetics as it does not natively support date data types; go for something else like PowerShell, VBScript, JavaScript, etc. – aschipfl Aug 27 '18 at 17:58

1 Answers1

0

I usually ignore "questions" that did not show any effort from the OP. However, I do an exception in this case, but I did not show any effort to explain the code...

@echo off
setlocal EnableDelayedExpansion

rem Define the "Date to Julian Day Number" and vice versa conversion functions
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 )"

rem Get the date of first and last mondays in the year
set /A "first=!DateToJDN(YMD):YMD=%10101!, first-=first%%7, last=first+371"

rem Show the date of all mondays
for /L %%a in (%first%,7,%last%) do (
   set /A "D=!JDNtoDate(JDN):JDN=%%a!"
   if "!D:~0,4!" equ "%1" echo !D:~0,4!-!D:~4,2!-!D:~6,2!
)

Place the input year in the Batch file parameter. Further details at this answer

Aacini
  • 65,180
  • 12
  • 72
  • 108