Change Date Ouput Format in Windows Batch Script

by Oscar
Published: Last Updated on

When echo %DATE, by default you get dd/mm/yyyy. There is no built in formatting parameter that you can use, so you cannot change the date format unless you rearrange the output characters around.

For example to get yyyy-mm-dd as output, you can do this.

set Today=%Date:~6,4%_%Date:~3,2%_%Date:~0,2%

It basically means.

SET Today=%Date:~6,4%-%Date:~3,2%-%Date:~0,2%
                  ^^ ^       ^           ^
                  || |       |           |
               Year| Dash  Month        Day
                   |
                Number of chars

Of course the format depends on your local region settings, check %DATE% before you change numbers.

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.

3 comments

Pat 20th December 2019 - 6:17 pm

My comment a couple of hours ago needs correcting. I had nothing to do with the upgrade – it was caused by running a batch program as an “administrator” from my “standard” account. The Admin account had default “Regional” settings. Once the date was set to the same format in both accounts, the date command began working again.
A regional setting of 19-12-20 creates a date 191220 from this command:
set Date=%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%

Reply
Pat 20th December 2019 - 2:50 pm

Here’s a strange one. Before upgrading to Windows 1909 this command produced 191220:
set Date=%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%
Since upgrading, a BAT with the exact same command produces 209-2-20.
My date setting is yy-mm-dd.P

Reply
Crazy 7th November 2019 - 2:53 am

How to change date format with dd/MM/yyyy?

Reply