Skip to main content

How to Get the Day of the Year (Julian Day) in Batch Script

The Day of the Year (also called the Ordinal Day or Julian Day of the calendar) is a number from 1 to 365 (or 366 in a leap year) representing how far into the current year a specific date is. For example, January 1 is Day 1, February 1 is Day 32, and December 31 is Day 365. This is widely used in satellite data, agricultural planning, and standardized date codes.

In this guide, we will demonstrate how to calculate the day of the year for any given date.

Method 1: The PowerShell Bridge (Simplest)

PowerShell provides a built-in .DayOfYear property.

Implementation Script

@echo off
setlocal

:: Get today's day of the year
for /f "usebackq delims=" %%A in (`
powershell -NoProfile -Command "(Get-Date).DayOfYear"
`) do set "doy=%%A"

echo Today is Day %doy% of the year.
pause
Specific Date

To get the day of the year for a specific date instead of today, replace (Get-Date) with ([datetime]'2024-02-15') in the PowerShell command.

Method 2: Pure Batch (Cumulative Days Array)

To calculate this in native Batch, we use a pre-defined array of "Days Elapsed" per month and add the current day.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Get the Date (from WMIC for reliability)
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set "dt=%%I"
set "yy=!dt:~0,4!"
set "mm=!dt:~4,2!"
set "dd=!dt:~6,2!"

:: Strip leading zeros for math (1%mm% - 100 converts "08" to 8 safely)
set /a "month=1!mm! - 100"
set /a "day=1!dd! - 100"
set /a "year=!yy!"

:: 2. Cumulative days before each month (non-leap year)
set "CUM_1=0" & set "CUM_2=31" & set "CUM_3=59"
set "CUM_4=90" & set "CUM_5=120" & set "CUM_6=151"
set "CUM_7=181" & set "CUM_8=212" & set "CUM_9=243"
set "CUM_10=273" & set "CUM_11=304" & set "CUM_12=334"

:: 3. Calculate the Day of the Year
set /a "doy=!CUM_%month%! + day"

:: 4. Leap Year Adjustment (add 1 if month > 2 and it's a leap year)
if !month! GTR 2 (
set /a "mod4=year %% 4"
set /a "mod100=year %% 100"
set /a "mod400=year %% 400"

set "isLeap=0"
if !mod4! EQU 0 set "isLeap=1"
if !mod100! EQU 0 set "isLeap=0"
if !mod400! EQU 0 set "isLeap=1"

if !isLeap! EQU 1 set /a "doy+=1"
)

echo.
echo ==========================================
echo DATE: !yy!-!mm!-!dd!
echo DAY OF YEAR: !doy!
echo ==========================================
pause
WMIC Deprecation

wmic is deprecated in newer versions of Windows. As a future-proof alternative, you can retrieve the date components using PowerShell instead:

for /f "usebackq delims=" %%I in (`powershell -NoProfile -Command "Get-Date -Format 'yyyyMMdd'"`) do set "dt=%%I"

Why Get the Day of the Year?

  1. File Naming: Using the Julian Day code (e.g., 2024-045) is a compact, sortable way to name daily log files or backups.
  2. Scheduling: Calculating if today is within a specific range (e.g., "Between Day 90 and Day 180") for seasonal maintenance windows.
  3. Data Standards: Many scientific and satellite data formats use the Day of the Year as their primary timestamp.

Important Considerations

Leap Years

February has 29 days in a leap year, pushing all subsequent months forward by 1 day. The script checks for this only when the month is greater than 2. A year is a leap year if it is divisible by 4, except for century years, which must also be divisible by 400.

Leading Zero Octal Trap

Batch treats numbers with leading zeros as octal. The month value 08 and 09 from date strings are invalid octal numbers and cause errors with set /a. The trick set /a "month=1%mm% - 100" prepends a 1 to create a valid decimal number (e.g., 108), then subtracts 100 to get the correct value (8).

Day Zero

There is no "Day 0." January 1 is always Day 1, and December 31 is Day 365 (or 366 in a leap year).

Conclusion

Calculating the Day of the Year provides a compact, numeric representation of any calendar date. Whether you use the simple PowerShell property or the robust native Batch calculation, this ordinal numbering system simplifies date comparisons, file naming, and scheduling logic. It is an essential tool for any administrator managing time-sensitive operations across the calendar year.