Solved How to check for Julian date in batch script

August 1, 2014 at 10:02:21
Specs: Windows 7
Good Morning,

Can anyone help me please to determine julian date in batch script.

Actually my job runs on every friday. The script has to look for a folder which has the current friday julian date, For example today is 08/01/2014 and the julian date is 213. My script should look for 213 folder. Please help me to capture this in batch script


See More: How to check for Julian date in batch script

Report •

✔ Best Answer
August 4, 2014 at 09:23:51
my bad! my apologies. I had it right in my working script, but posted incorrect. 6th line up from the bottom:
set x=2+leap
should be:
set /a x=2+leap
I fixed it in my post (#8) as well.

message edited by nbrane



#1
August 1, 2014 at 13:06:40
::-------- begin batch juldate.bat. ONLY works on date format XXX mm/dd/year!
@echo off & setlocal
set m=/%date:~4,2%
set d=%date:~6,3%
:: zero {year modulo 4} stays zero. 1,2 or 3 all shake out to one.
set /a leap=(%date:~10%"%%"4*1+2)/3
:: dodge the octal trap
set m=%m:/0=%
set d=%d:/0=%
set /a dd=(m-1)*31+d-leap
::echo leap: %leap% m: %m% d: %d%
set x=2
for %%a in (2 4 6 9 11) do (
if %m% gtr %%a set /a dd-=x
set x=1
)
echo julian date: %dd%
::------- end batch
sort-of tested using prompted date instead of %date%.
If you need to handle a different date format, let me know what that is and I'll try and accomodate.

message edited by nbrane


Report •

#2
August 1, 2014 at 20:46:36
Bug: Gives wrong date starting March in year 2100.
Bug: Gives wrong date before March in non-leap years.

Really, the other languages both have current day of year built in. There's no reason to hobble yourself with batch.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#3
August 1, 2014 at 20:55:38
Avoids date layout mess by using CMOS.

:: lesson learned: set/a 1M=31 gives invalid # but set/a M1=31 OK !!!
::
::
::
::====== script starts here ===============
:: IGNORES leap years
:: jul.bat 2014-08-02 9:04:36.41
@echo off

> d.com echo ´*Í!ˆÐ´LÍ!
> m.com echo ´*Í!ˆð´LÍ!
setLocal enableDELAYedeXpansioN

set/a M1=31 & set/a M2=59 & set/a M3=90 & set/a M4=120 & set/a M5=151
set/a M6=181 & set/a M7=212 & set/a M8=243 & set/a M9=273 & set/a M10=304
set/a M11=334

M & set/a M=!errorlevel!-1
D & set/a JUL=!M%M%!+!errorlevel! & echo JUL is !JUL!

del d.com & del m.com
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
August 1, 2014 at 21:00:17
Posting again with html tags:

:: lesson learned: set/a 1M=31 gives invalid # but set/a M1=31 OK !!!
::
::
::
::======  script starts here  ===============
:: IGNORES leap years
:: jul.bat  2014-08-02  9:04:36.41
@echo off

> d.com echo ´*Í!ˆÐ´LÍ!
> m.com echo ´*Í!ˆð´LÍ!
setLocal enableDELAYedeXpansioN

set/a M1=31 & set/a M2=59 & set/a M3=90 & set/a M4=120 & set/a M5=151
set/a M6=181 & set/a M7=212 & set/a M8=243 & set/a M9=273 & set/a M10=304
set/a M11=334

M & set/a M=!errorlevel!-1
D & set/a JUL=!M%M%!+!errorlevel! & echo JUL is !JUL!

del d.com & del m.com
::======  script ends here  =================

=====================
M2 Golden-Triangle


Report •

#5
August 1, 2014 at 21:05:54
Well, It got mangled.

IIRR win7 does not have debug. Correct?

=====================
M2 Golden-Triangle


Report •

#6
August 1, 2014 at 21:19:27
x64 does not have debug. If you're using 32-bit Win7, you have debug. If you have x64 WinXP, you haven't had debug for a long time.

How To Ask Questions The Smart Way


Report •

#7
August 1, 2014 at 22:12:10
Hi R2,
Next on our list of embarassing questions: will debug WORK in 64 bit?

=====================
M2 Golden-Triangle


Report •

#8
August 1, 2014 at 22:46:44
HA ha! Yeh, too many short-cuts etc. and not tested enough. Thanks, Raz!
(I was going to go with VBS, but backed off in case OP wants batch-only)
I finally lowered the flag on Debug, much as I hated to. I will still summon the "old ones" in my own 32-bit pursuits.
Here is my attempted batch fix to #1:

@echo off & setlocal
:: NOTE: the leading space IS signficant/critical in foll. two lines
set m= %date:~4,2%
set d= %date:~7,2%
:: zero {year modulo 4} stays zero. 1,2 or 3 all shake out to one.
set /a leap=(%date:~10%"%%"4*1+2)/3
:: dodge the octal trap
set m=%m: 0=%
set d=%d: 0=%
::edited foll. two lines - still got it wrong last night!
set /a dd=(m-1)*31+d
::echo leap: %leap% m: %m% d: %d%
set /a x=2+leap
for %%a in (2 4 6 9 11) do (
if %m% gtr %%a set /a dd-=x
set x=1
)
echo julian date: %dd%
::------- end batch
did quick tests...
(but not enough, I still had it wrong for january and february. I repaired that bug in this post.)

message edited by nbrane


Report •

#9
August 2, 2014 at 03:01:13
I put up the 2 small com utilities; dunno if they work in 64 bit LOL.

http://www.golden-triangle.com/CMOS...

=====================
M2 Golden-Triangle


Report •

#10
August 2, 2014 at 05:09:04
No, debug doesn't work in x64. The DOS emulation layer just isn't there anymore.

As for my stab, batch calling powershell. Should work in Win7+, and maybe Vista:

for /f "usebackq" %%a in (`powershell -Command [DateTime]::Today.DayOfYear.ToString('000'^)`) do set julian=%%a
echo %julian%

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#11
August 2, 2014 at 12:46:50
@M2: You might want to escape those exclams in lines two and three. (Or, alternatively, explicitly disable delayed expansion). My system defaults to delayedexpansion enabled, which messed up the output from the echos on those lines (leaving a 5-byte .com file instead of 12). Nice dodge around the DEBUG issue, but I still don't know if those coms will run on a 64-bit. I have a 64 somewhere, but never use it so I dont know which computer has it. You could also add a subtraction of 7DC from reg. CX to synchronize your year with leaps (but necessitating a 3rd .com of course).

message edited by nbrane


Report •

#12
August 4, 2014 at 07:22:16
I tried testing the below script. It gives the output as julian date: 217
but today is 216 as per julian date, Can u please look into this. Thanks for ur support

@echo off & setlocal
:: NOTE: the leading space IS signficant/critical in foll. two lines
set m= %date:~4,2%
set d= %date:~7,2%
:: zero {year modulo 4} stays zero. 1,2 or 3 all shake out to one.
set /a leap=(%date:~10%"%%"4*1+2)/3
:: dodge the octal trap
set m=%m: 0=%
set d=%d: 0=%
::edited foll. two lines - still got it wrong last night!
set /a dd=(m-1)*31+d
::echo leap: %leap% m: %m% d: %d%
set x=2+leap
for %%a in (2 4 6 9 11) do (
if %m% gtr %%a set /a dd-=x
set x=1
)
echo julian date: %dd%


Report •

#13
August 4, 2014 at 09:23:51
✔ Best Answer
my bad! my apologies. I had it right in my working script, but posted incorrect. 6th line up from the bottom:
set x=2+leap
should be:
set /a x=2+leap
I fixed it in my post (#8) as well.

message edited by nbrane


Report •

#14
August 4, 2014 at 09:33:45
Thanks a lot its working now :)

Report •

#15
August 4, 2014 at 18:19:36
@echo off & setlocal
set dt=%date%
set /a m=1%dt:~4,2%-100
:: wanted to try this. it seemed to work.
set /a leap=(%dt:~10%"%%"4*1+2)/3,dd=(m-1)*31+1%dt:~7,2%-100,x=2+leap
for %%a in (2 4 6 9 11) do (
if %m% gtr %%a set /a dd-=x
set x=1
)
echo julian date: %dd%

message edited by nbrane


Report •

Ask Question