Format Windows command line date

The need for me to format date in the Windows command line environment came up when I needed to write a batch file to automatically copy the latest copies of certain files in a large folder. The files are named in the format of ???YYYYMMDD.txt, which presented itself as one easy way for me to query for latest files.

Built in to Windows command line is the %date% variable, which displays the system date as follows based on my regional setting.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:>echo %date%
Wed 10/08/2008

Please note that your regional settings might be different. For example, for those in Australia, the date might be presented in a DD/MM/YYYY format rather than the typical US MM/DD/YYYY format. If this is the case, you may wish to adjust the code below accordingly.

To suit my needs, I need to format this date to YYYYMMDD. Knowing that %date%’s format is consistent in format, we can just parse it as a string.

C:>echo %date:~10,4%
2008

C:>echo %date:~4,2%
10

C:>echo %date:~7,2%
08

Bringing it together, I will have the YYYYMMDD format I am looking for.

C:>echo %date:~10,4%%date:~4,2%%date:~7,2%
20081008

Once again, you may need to adjust the above command based on your own regional settings.

Here is how I put this code in action in the form of a batch file that copied only the files with today’s date.

set backupcmd=c:windowssystem32xcopy.exe /c /d /e /h /r /y
set dt=%date:~10,4%%date:~4,2%%date:~7,2%
%backupcmd% C:stuff*%dt%.txt D:backupstuff

11 Replies to “Format Windows command line date”

    1. Thanks for visiting Dev-Notes, Len. This how string parsing works in Windows command line, assuming your variable is named %date%:

      %date:~10,4% – Start with the 10th character of the string, and include the subsequent 4 characters of the string.

      This thus gives us the year value out of our previously stated example of “Wed 10/08/2008”.

      Also, please keep in mind that when counting characters you should start with 0 rather than 1.

      Thanks again.

    2. It’s not an answer Len… it’s a blog post offering advice. How about saying thank you and doing some research yourself?

  1. This is nice and common knowledge.
    However, what if you do logging on a device per device base and then send this logging to one fileshare.
    The formatting is just a mess because different user have different regional settings in a large enterprise

  2. Thanks C. Peter Chen. I needed to write a cmd for a Windows server that would archive log files with date-time stamps whenever my application is stop-started. Your example above got me started on the right foot. Below is the date-time string I’m using, thanks to you:

    %date:~10,4%.%date:~4,2%.%date:~7,2%.%time:~0,2%.%time:~3,2%.%time:~6,2%
    2020.04.29.14.30.21

  3. Hi team ,

    i want to print my output
    abc_ddmmyyyy_00:00.txt formate
    then how can write in batch file to get like this type output.
    Saurabh Sharma

  4. This doesn’t work. I have different regional settings in different accounts and the following is output in them:

    >echo %date%
    03.04.2021

    >echo %date%
    Sat 04/03/2021

  5. If you want a regional independent, you have to use powershell:
    POWERSHELL -EXECUTIONPOLICY Bypass -NOLOGO -NONINTERACTIVE -NoProfile -COMMAND “Get-Date -format ‘yyyy-MM-dd HH:mm:ss tt'”
    It uses the C# format

    If you want to capture in Cmd variable, you have to create a function like this:
    :GetCurrentDate format returnDate
    Setlocal EnableExtensions EnableDelayedExpansion
    Set “DateFormat=%~1”
    Set “CurrentDateVar= ”
    For /f “tokens=*” %%i In (‘POWERSHELL -EXECUTIONPOLICY Bypass -NOLOGO -NONINTERACTIVE -NoProfile -COMMAND “Get-Date -format ‘%DateFormat%'”‘) Do (
    Set “CurrentDateVar=%%i”
    )
    Endlocal & (
    ::Set return variables
    IF “%~2” NEQ “” (
    Set “%~2=%CurrentDateVar%”
    ) Else (
    Echo.[%CurrentDateVar%]
    )
    )
    Goto:EOF
    How to use:
    Call:GetCurrentDate “yyyy-MM-dd HH:mm:ss.fff tt” dateReturned
    Echo %dateReturned%

Leave a Reply to Alex Cancel reply

Your email address will not be published. Required fields are marked *