how to set date to current date using dos batch file command.
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.
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
When you type "date" on the dos command it will show you the date specified in your bios, and ask you to set the current date (you can set this if the date in your bios is not the current). The system has no ability to know if it's date is correct.
If you have a server with correct date, you can use "net time" so the the client can synchronize with the server.
NET TIME <SERVERNAME> /SET