## Re: loop with date

Expand Messages
• ... Hi. I am newbie to this group. I browse for old posts and found that nobody had answered to this topic, so this is my answer. The Julian Day Number is a
Message 1 of 3 , Jun 2, 2011
--- In batchworld@yahoogroups.com, "Abhijat Singh" <mail_abhijat@...> wrote:
>
> Hi Group,
> Hope all of you are doing well. I need some help in looping over date.
> I need something euivalent to this
> for date = 01/01/2010 to 12/31/2010 but it should only loop 365 times and not (20101231 - 20100101) 1130 times.
>
> Thanks very much!
>
> Kind Regards,
> Abhijat
>

Hi. I am newbie to this group. I browse for old posts and found that nobody had answered to this topic, so this is my answer.

The Julian Day Number is a uniform number of days from a distant year (before 4000 BC) that is useful for this type of calculations. The following Batch file convert a date to Julian Day Number and return it in JDN variable.

DATETOJULIAN.BAT

@ECHO OFF
REM Convert a Date to Julian Day Number
REM Reference: http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html

REM GET DAY, MONTH, YEAR VALUES
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C

REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100

REM CALCULATE JULIAN DAY NUMBER AND DAY OF WEEK
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524, DOW=(JDN+1)%%7

This way, to do what you need:

CALL DATETOJULIAN 01/01/2010
SET START=%JDN%
CALL DATETOJULIAN 12/31/2010
SET END=%JDN%
FOR /L %%D IN (%START%,1,%END%) DO ...

Or, to use loop values starting from 1:

SET /A RANGE=END-START+1
FOR /L %%I IN (1,1,%RANGE%) DO ...

Julian Day Number may also be used to calculate the Day Of Week of a given date, that is returned in DOW variable:

CALL DATETOJULIAN %DATE%
IF %DOW% == 0 ECHO Today is Sunday!

Regards...
Antonio
• ... This is great stuff! Thanks Antonio. I just added JDN-=2415019 to get the Julian date comparable to what we get in excel. Based on this snippet and the
Message 2 of 3 , Jun 21, 2011
--- In batchworld@yahoogroups.com, "aaciini" <antonio.perez.ayala@...> wrote:
>
> --- In batchworld@yahoogroups.com, "Abhijat Singh" <mail_abhijat@> wrote:
> >
> > Hi Group,
> > Hope all of you are doing well. I need some help in looping over date.
> > I need something euivalent to this
> > for date = 01/01/2010 to 12/31/2010 but it should only loop 365 times and not (20101231 - 20100101) 1130 times.
> >
> > Thanks very much!
> >
> > Kind Regards,
> > Abhijat
> >
>
>
>
> Hi. I am newbie to this group. I browse for old posts and found that nobody had answered to this topic, so this is my answer.
>
> The Julian Day Number is a uniform number of days from a distant year (before 4000 BC) that is useful for this type of calculations. The following Batch file convert a date to Julian Day Number and return it in JDN variable.
>
> DATETOJULIAN.BAT
>
> @ECHO OFF
> REM Convert a Date to Julian Day Number
> REM Reference: http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html
>
> REM GET DAY, MONTH, YEAR VALUES
> FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
>
> REM ELIMINATE LEFT ZEROS
> SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
>
> REM CALCULATE JULIAN DAY NUMBER AND DAY OF WEEK
> IF %MM% LSS 3 SET /A MM+=12, YY-=1
> SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524, DOW=(JDN+1)%%7
>
> This way, to do what you need:
>
> CALL DATETOJULIAN 01/01/2010
> SET START=%JDN%
> CALL DATETOJULIAN 12/31/2010
> SET END=%JDN%
> FOR /L %%D IN (%START%,1,%END%) DO ...
>
> Or, to use loop values starting from 1:
>
> SET /A RANGE=END-START+1
> FOR /L %%I IN (1,1,%RANGE%) DO ...
>
> Julian Day Number may also be used to calculate the Day Of Week of a given date, that is returned in DOW variable:
>
> CALL DATETOJULIAN %DATE%
> IF %DOW% == 0 ECHO Today is Sunday!
>
> Regards...
> Antonio
>
This is great stuff! Thanks Antonio. I just added JDN-=2415019 to get the Julian date comparable to what we get in excel. Based on this snippet and the information under http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html, I will also get a Julian to Date program set up for me.

Thanks again!
Kind Regards,
Abhijat.
Your message has been successfully submitted and would be delivered to recipients shortly.