Batch script to show day of the year?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chcfrank
Posts: 10
Joined: 22 May 2008 20:24

Batch script to show day of the year?

#1 Post by chcfrank » 25 May 2008 09:11

Any ideas(simpler) to calculate day of the year?
What I can come up so far is to:
1. use 'map and lookup' idea by creating a string which maps the month to total number of days in previous month like:
set days=01-00;02-31;03-59;04-90 ...
2. add the resulting value from month to the day value
3. adjust the result by checking whether it is leap year or not

-Frank

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 15 Jun 2008 23:58

chcfrank,

Here is a new function.
http://www.dostips.com/DtCodeCmdLib.php ... _dayOfYear

Code: Select all

@ECHO OFF

call:dayOfYear day 1/1/2008
echo.%day%

call:dayOfYear day "%date%"
echo.%day%

call:dayOfYear day 12/31/2008
echo.%day%

GOTO:EOF


rem insert :dayOfYear function here ....

Output:

Code: Select all

1
168
366

DOS IT HELP? :wink:

miskox
Posts: 672
Joined: 28 Jun 2010 03:46

Re: Batch script to show day of the year?

#3 Post by miskox » 17 Sep 2025 22:39

VBS:

Code: Select all

@echo off
set yyyy=2025
set mm=12
set dd=31
>tmp.vbs echo wscript.stdout.write 1+DateDiff("y", "%yyyy%-01-01", "%yyyy%-%mm%-%dd%")
for /f %%A In ('cscript tmp.vbs //noLogo') do set doy=%%A
if exist tmp.vbs del tmp.vbs

echo %doy%
Saso

Aacini
Expert
Posts: 1935
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch script to show day of the year?

#4 Post by Aacini » 18 Sep 2025 11:31

Some time ago I posted the simple Batch code below at this S.O. answer,

Code: Select all

@echo off
setlocal

set "daysPerMonth=0 31 28 31 30 31 30 31 31 30 31 30"

for /F "tokens=1-3" %%a in ('wmic Path Win32_LocalTime Get Day^,Month^,Year') do (
   set /A "dayOfYear=%%a, month=%%b, leap=!(%%c%%4)*(((month-3)>>31)+1)" 2>NUL
)
set /A "i=1, dayOfYear+=%daysPerMonth: =+(((month-(i+=1))>>31)+1)*%+leap"

echo %dayOfYear%
This code uses the wmic inside a for /F to get today's date. If you already have the date in day, month, year variables, the code is simpler and can be completed in a single, long line:

Code: Select all

set /A "leap=!(year%%4)*(((month-3)>>31)+1), i=1, dayOfYear=day+%daysPerMonth: =+(((month-(i+=1))>>31)+1)*%+leap"
Antonio

miskox
Posts: 672
Joined: 28 Jun 2010 03:46

Re: Batch script to show day of the year?

#5 Post by miskox » 18 Sep 2025 12:42

Very good Antonio! Your solutions are always so clever (short).

Saso

Aacini
Expert
Posts: 1935
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch script to show day of the year?

#6 Post by Aacini » 18 Sep 2025 14:47

I just devised a simpler method! :D

Code: Select all

@echo off
setlocal

for /F "tokens=1-3" %%a in ('wmic Path Win32_LocalTime Get Day^,Month^,Year') do (
   set /A "day=%%a, month=%%b, year=%%c" 2> NUL
)

set /A "dayOfYear=day + (month-1)*30 + (month+month/9)/2 - (((month-3)>>31)+1)*(2-!(year%%4))"

echo %dayOfYear%
Antonio

Post Reply