Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Sat 17-Apr-2010 15:15:44

Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.

148} How to calculate the day-of-year number with a pure cmd script?

  @echo off & setlocal enableextensions disabledelayedexpansion
  if "%~3"=="" (
    echo Usage: %~0 DD MM YYYY
    echo No leading zeros!
    goto :EOF)
  :: Get the date

  set day=%~1
  set month=%~2
  set year=%~3
  :: Call the day ordinal number subroutine

  call :JDdayNumber %day% %month% %year% DayOrdinalNumber
  :: Display the result

  echo %day%.%month%.%year% Day ordinal number %DayOrdinalNumber%
  endlocal & goto :EOF
  :: ============================================================
  :: 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

The output might be e.g.
  C:\_D\TEST>cmdfaq 17 9 2006
  17.9.2006 Day ordinal number 260


An alternative, more concise formulation of the ordinal day subroutine
:: ============================================================
:: Subroutine: Calculate a day's ordinal number within the year
:: Based on a posting by Herbert Kleebauer

:JDdayNumber d m y w
setlocal enableextensions disabledelayedexpansion
set /a w= %1 +(!(%3 %% 4)-!(%3 %% 100)+!(%3 %% 400))*(!((%2-3)^&16))
set /a w=(%w%+(%2-1)*30+2*(!((%2-7)^&16))-1+((65611044^>^>(2*%2))^&3))
endlocal & set "%4=%w%" & goto :EOF

In the above
  ! Unary NOT operator
  & Bitwise AND
  %% Remainder (modulo) operator
  >> Logical shift

References/Comments: (If a Google message link fails try the links within the brackets.)
  Also see item #70.
  How can I calculate the week number?
  Google Groups Sep 8 2006, 1:39 pm [M]