148} How to calculate a day's ordinal number with a pure cmd script?
@echo off & setlocal enableextensions
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 the Julian day ordinal number
:JDdayNumber day month year return_
setlocal enableextensions enabledelayedexpansion
if %2 LEQ 2 (
set /a a=%3-1
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 e=0
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 e=!s!+1
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
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
You don't use the result of the above 5 lines at all.
> 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 e=!s!+1
You don't use the result of the above line.
> 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
>
> All the best, Timo
You don't even need to distinct between month <=2 and month > 2 :
@echo off
setlocal disabledelayedexpansion
set /a y=2006
set /a m=9
set /a d=17
set /a w= %d% +(!(%y% %% 4)-!(%y% %% 100)+!(%y% %% 400))*(!((%m%-3)^&16))
set /a w=(%w%+(%m%-1)*30+2*(!((%m%-7)^&16))-1+((65611044^>^>(2*%m%))^&3))
echo %w%
[]
>>
>> The output might be e.g.
>> C:\_D\TEST>cmdfaq 17 9 2006
>> 17.9.2006 Day ordinal number 260
>>
>> All the best, Timo
>
>
> You don't even need to distinct between month <=2 and month > 2 :
>
> @echo off
> setlocal disabledelayedexpansion
>
> set /a y=2006
> set /a m=9
> set /a d=17
>
> set /a w= %d% +(!(%y% %% 4)-!(%y% %% 100)+!(%y% %% 400))*(!((%m%-3)^&16))
> set /a w=(%w%+(%m%-1)*30+2*(!((%m%-7)^&16))-1+((65611044^>^>(2*%m%))^&3))
> echo %w%
I did something like this in asm, as dos batch doesn't have the arithmetic.
I'm afraid the code got overoptomised along the way, or I'd extract &
post the relevant bits (pun).
> You don't use the result of the above 5 lines at all.
The lines indeed are superfluous. They are a leftover from the
similar weeknumber calculations posted subsequently.
> You don't even need to distinct between month <=2 and month > 2 :
> set /a w= %d% +(!(%y% %% 4)-!(%y% %% 100)+!(%y% %% 400))*(!((%m%-3)^&16))
> set /a w=(%w%+(%m%-1)*30+2*(!((%m%-7)^&16))-1+((65611044^>^>(2*%m%))^&3))
> echo %w%
That's very neat. Thank you. Written as a subroutine:
:: ===================================================
:: Subroutine: Calculate the Julian day ordinal number (H.K.)
:JDdayNumber2 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
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
While that is correct terminology by ISO 8601, "day-of-year" or "day-of-
year number" might be more widely understood.
> :: Subroutine: Calculate the Julian day ordinal number
Julian does not apply. The answer depends only on Day, Month, and
Leapness of Year; and Leapness if not needed if M<3. You use, of
course, the Gregorian Leapness Algorithm.
It might be better to produce a Leapness algorithm, to be called as such
from other code.
Be thankful you're in Finance & not History - a Finnish
historian might need a Leapness algorithm handling
Julian, Gregorian, and the Swedish early 1700s.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
>set /a w= %d% +(!(%y% %% 4)-!(%y% %% 100)+!(%y% %% 400))*(!((%m%-3)^&16))
>set /a w=(%w%+(%m%-1)*30+2*(!((%m%-7)^&16))-1+((65611044^>^>(2*%m%))^&3))
>echo %w%
In Munich, and not using the more-or-less locally-produced Zeller's
Congruence?
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm zeller-c.htm etc.
> While that is correct terminology by ISO 8601, "day-of-year" or "day-of-
> year number" might be more widely understood.
148} How to calculate the day-of-year number with a pure cmd script?
>> :: Subroutine: Calculate the Julian day ordinal number
:: Subroutine: Calculate a day's ordinal number
> Be thankful you're in Finance & not History - a Finnish
> historian might need a Leapness algorithm handling
> Julian, Gregorian, and the Swedish early 1700s.
:-) In the case of a batch FAQ, only dates after 1980 are truly
relevant. A typical need is for, say, calculating the age of a file
from today, and so on.
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
>:-) In the case of a batch FAQ, only dates after 1980 are truly
>relevant. A typical need is for, say, calculating the age of a file
>from today, and so on.
Indeed : but it would be boring if we were all always typical.
I often do, at the command line, such as
mjd_date 1867 10 18
-> CivilDate B AD 1867-10-18 YMD->MJD: 3257 Fri MJD->YMD: AD 1867-10-18
(That's actually Pascal; those should really be CMJD, but space was tight)
BTW, while a CMJD is normally 24 h, sometimes +- one hour and +(-) one
second, in Alaska that particular CMJD was 48 hours long.
JulianCal AD 1867-10-06 YMD->MJD: 3257 Fri MJD->YMD: AD 1867-10-06
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.