Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set date to current date using dos batch file command

Tags:

batch-file

dos

how to set date to current date using dos batch file command.

like image 706
silverkid Avatar asked Sep 03 '26 02:09

silverkid


2 Answers

If you need to use the current date in a batch file, the variable %date% has the current date:

echo %date%
23/02/2010

It uses the format of the regional setting of your computer. In my computer it's dd/mm/yyyy.

Since the / can't be part of a file name, they must be replaced with a safe character or nothing:

echo %date:/=-%
23-02-2010

echo %date:/=%
23022010

If you want to create a backup copy of a file, you can do something like:

copy file.txt file-%date:/=%.txt
dir /b file*.*
file-23022010.txt
file.txt

Or first set it to a variable and then use it:

set currdate=%date:/=%
copy file.txt file-%currdate%.txt
like image 84
Carlos Gutiérrez Avatar answered Sep 06 '26 00:09

Carlos Gutiérrez


The date command is what you are looking for. This works on my Windows XP box:

date 15-02-2010

Notice the formatting dd-MM-yyyy, which seems to be required here, probably because of my regional settings being set to Denmark. The documentation states that the format is MM-dd-yy, but on my computer, the day and month fields gets flipped if the date is written in that format.

like image 45
Jørn Schou-Rode Avatar answered Sep 06 '26 02:09

Jørn Schou-Rode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!