Solved Solution for displaying JDAY batch

March 31, 2016 at 10:38:24
Specs: Windows 7, 8GB
Ok, I needed a batch file to display the current JDAY for an DoD operations center. You can double-click the file and it will display in a CMD window. Then, press any key to exit.

Here's what I came up with:

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

:: Call the day ordinal number subroutine
call :JDdayNumber %DD% %MM% %YYYY% DayOrdinalNumber

:: Display the result
echo.
echo Today is JDay %DayOrdinalNumber%
echo.
pause,
endlocal & goto :EOF

:: set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
:: set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
:: echo datestamp: "%datestamp%"
:: echo timestamp: "%timestamp%"
:: echo fullstamp: "%fullstamp%"

:: ============================================================
:: Subroutine: Calculate a day's ordinal number within the year
:JDdayNumber day month year return_
setlocal enableextensions enabledelayedexpansion
if %2 LEQ 2 (
set /a f=%1-1+31*^(%2-1^)
) else (
set /a a=%3
set /a b=!a!/4-!a!/100+!a!/400
set /a c=^(!a!-1^)/4-^(!a!-1^)/100+^(!a!-1^)/400
set /a s=!b!-!c!
set /a f=%1+^(153*^(%2-3^)+2^)/5+58+!s!
)
set /a return_=%f%+1
endlocal & set "%4=%return_%" & goto :EOF


See More: Solution for displaying JDAY batch

Report •

✔ Best Answer
March 31, 2016 at 22:18:51
Here's a possible "pure batch" based on date format: DOW MM DD YY[YY]
Like: Fri 04/01/2016
@echo off & setlocal
:: this is a test-loop - use "x" to escape
:1
set dt=
set /p dt=date:
if /i "%dt%" equ "x" goto :eof
:: defaults to my date format as described above
if not defined dt set dt=%date:~4%
::------ core
:: method attributed to Razor for removing leading zeros
set /a m=1%dt:~0,2%-100
set /a d=1%dt:~3,2%-100
rem echo m: %m%
set /a leap=(%dt:~6%"%%"4+2)/3, dd=(m-1)*31+d, x=2+leap
rem echo dd: %dd%
for %%a in (2 4 6 9 11) do (
if %m% gtr %%a set /a dd-=x
set x=1
)
echo jul date: %dd%
goto :1
::---- end
worked ok for my tests, but... no date-validation. This assumes input date is a valid date! F/e: 02/29/2015 will not make an error. Also assumes that day and month elements are zero-padded to two places. Hence the preference toward powershell or vbscript solutions.

message edited by nbrane



#1
March 31, 2016 at 12:52:51
I mean if you're looking for the number of days that has passed so far this year, you're probably better off using VBScript or PowerShell. Or using a batch file to call one of the above.
powershell -c "(Get-Date).DayOfYear.ToString('000')"

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#2
March 31, 2016 at 19:32:55
Can you put that in a .bat file? I cannot install any .exe files of any kind on the DoD system.

Report •

#3
March 31, 2016 at 20:15:49
Sure?
powershell -c "(Get-Date).DayOfYear.ToString('000')"
pause

If you're using Vista or lower, you'll need to use VBScript (.vbs file) instead.

WScript.Echo Right("00" & DatePart("y" , Date), 3)

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
March 31, 2016 at 20:17:14
Wow, I'll give it a shot tomorrow at work. Thank you! I am pretty new to this!

Report •

#5
March 31, 2016 at 22:18:51
✔ Best Answer
Here's a possible "pure batch" based on date format: DOW MM DD YY[YY]
Like: Fri 04/01/2016
@echo off & setlocal
:: this is a test-loop - use "x" to escape
:1
set dt=
set /p dt=date:
if /i "%dt%" equ "x" goto :eof
:: defaults to my date format as described above
if not defined dt set dt=%date:~4%
::------ core
:: method attributed to Razor for removing leading zeros
set /a m=1%dt:~0,2%-100
set /a d=1%dt:~3,2%-100
rem echo m: %m%
set /a leap=(%dt:~6%"%%"4+2)/3, dd=(m-1)*31+d, x=2+leap
rem echo dd: %dd%
for %%a in (2 4 6 9 11) do (
if %m% gtr %%a set /a dd-=x
set x=1
)
echo jul date: %dd%
goto :1
::---- end
worked ok for my tests, but... no date-validation. This assumes input date is a valid date! F/e: 02/29/2015 will not make an error. Also assumes that day and month elements are zero-padded to two places. Hence the preference toward powershell or vbscript solutions.

message edited by nbrane


Report •

Ask Question