Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to calculate a day's ordinal number with a pure cmd script?

1,021 views
Skip to first unread message

Timo Salmi

unread,
Sep 8, 2006, 5:48:53 AM9/8/06
to
DRAFT

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

Herbert Kleebauer

unread,
Sep 8, 2006, 7:39:46 AM9/8/06
to

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%

Marcus Red

unread,
Sep 8, 2006, 8:18:26 AM9/8/06
to
Herbert Kleebauer wrote:
> Timo Salmi wrote:
>> DRAFT
>>
>> 148} How to calculate a day's ordinal number with a pure cmd script?


[]

>>
>> 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).

Timo Salmi

unread,
Sep 8, 2006, 9:51:38 AM9/8/06
to
Herbert Kleebauer wrote:
> Timo Salmi wrote:
>> :: 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

> 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

Dr John Stockton

unread,
Sep 8, 2006, 4:01:21 PM9/8/06
to
JRS: In article <45013c87$0$17139$9b53...@news.fv.fi>, dated Fri, 8
Sep 2006 12:48:53 remote, seen in news:alt.msdos.batch.nt, Timo Salmi
<t...@uwasa.fi> posted :

>DRAFT
>
>148} How to calculate a day's ordinal number with a pure cmd script?

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.

Dr John Stockton

unread,
Sep 8, 2006, 4:06:40 PM9/8/06
to
JRS: In article <45015682...@unibwm.de>, dated Fri, 8 Sep 2006
13:39:46 remote, seen in news:alt.msdos.batch.nt, Herbert Kleebauer
<kl...@unibwm.de> posted :

>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.

Timo Salmi

unread,
Sep 9, 2006, 1:36:27 AM9/9/06
to
Dr John Stockton <j...@merlyn.demon.co.uk> wrote:
> Timo Salmi <t...@uwasa.fi> posted :

>>148} How to calculate a day's ordinal number with a pure cmd script?

> 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

Dr John Stockton

unread,
Sep 9, 2006, 6:21:53 PM9/9/06
to
JRS: In article <450252de$0$17128$9b53...@news.fv.fi>, dated Sat, 9 Sep
2006 08:36:27 remote, seen in news:alt.msdos.batch.nt, Timo Salmi
<t...@uwasa.fi> posted :

>:-) 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.

0 new messages