Share via

I need a way for a CMD file to determine the day number of the year

John Pollard 0 Reputation points
2026-02-21T02:36:56.3166667+00:00

My CMD file will copy a folder to one of two different devices/folders, alternating devices/folders depending on whether the current day number is odd or even.

I can't find a CMD that will give me the day number of the current year.

Is there one and I've just missed it? If not, is there a publicly available routine that I can use for my purpose?

I'm using 64bit Windows 11 Pro.

[Could not find Child Item for this question that appeared applicable, chose one randomly.]

Windows for home | Windows 11 | Files, folders, and storage
{count} votes

2 answers

Sort by: Most helpful
  1. Les Ferch 10,311 Reputation points Volunteer Moderator
    2026-02-25T19:55:39.87+00:00

    This would typically be done using a script. That is, a batch file, PowerShell script, JScript script, Python script etc. Do you have a script of some sort already? If so, please post it so others can help you get the desired result. If you have no script so far, well, then, you're at square one.

    The script the AI proposed below looks like an overly complicated way to determine if the current date is odd or even.

    Here's what ChatGPT came up with when I asked it create a concise batch file that determines if the current date is odd or even:

    @echo off
    
    for /f %%A in ('wmic os get LocalDateTime ^| find "."') do set dt=%%A
    set day=%dt:~6,2%
    
    set /a mod=day%%2
    
    if %mod%==0 (
        echo Even
    ) else (
        echo Odd
    )  
    

    It also gave me an ultra-compact version:

    @for /f %%A in ('wmic os get LocalDateTime ^| find "."') do @set d=%%A&set /a m=%d:~6,2%%%2&if %m%==0 (echo Even) else echo Odd
    

    Here's what it came up with for PowerShell:

    if((Get-Date).Day % 2){'Odd'}else{'Even'}
    

    These are all untested, so buyer beware, but the point is you can use AI yourself to get the answer and go back and forth to flesh out your script to do the copy based on the odd/even result, add error checking etc. As you can see, the PowerShell version is most concise and I would recommend that you use PowerShell rather than batch files to achieve your goals. It's more powerful (i.e. much more built in capability) and much more readable.

    0 comments No comments

  2. Q&A Assist
    2026-02-21T02:37:37.7833333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    In a CMD file, you can determine the day number of the year using a combination of commands. While there isn't a direct command to get the day number, you can use the date command along with some string manipulation to achieve this. Here's a method you can use:

    @echo off
    setlocal enabledelayedexpansion
    
    rem Get the current date
    for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (
        set month=%%a
        set day=%%b
        set year=%%c
    )
    
    rem Calculate the day of the year
    set /a "dayOfYear=day"
    
    rem Add days for the previous months
    for /L %%m in (1,1,!month!-1) do (
        set /a "dayOfYear+=!daysInMonth[%%m]!"
    )
    
    rem Define the number of days in each month
    set daysInMonth[1]=31
    set daysInMonth[2]=28
    set daysInMonth[3]=31
    set daysInMonth[4]=30
    set daysInMonth[5]=31
    set daysInMonth[6]=30
    set daysInMonth[7]=31
    set daysInMonth[8]=31
    set daysInMonth[9]=30
    set daysInMonth[10]=31
    set daysInMonth[11]=30
    set daysInMonth[12]=31
    
    rem Check for leap year
    if !year! %% 4 == 0 if !year! %% 100 neq 0 set /a "dayOfYear+=1"
    if !year! %% 400 == 0 set /a "dayOfYear+=1"
    
    echo Day of the year: !dayOfYear!
    
    rem Check if the day number is odd or even
    if !dayOfYear! %% 2 == 0 (
        echo Even day
    ) else (
        echo Odd day
    )
    endlocal
    

    This script retrieves the current date, calculates the day of the year, and checks if it is odd or even. You can then use this information to decide which folder to copy to.


    References:


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.