how to set date to current date using do开发者_运维知识库s batch file command.
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
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.
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
or
NET TIME \\SERVERNAME /SET /YES
This should work if you have the "Set time automatically" option on:
net stop w32time
net start w32time
w32tm /resync /nowait
Or you can save the current date to a text file and then pass the current date to the date command:
date /t>current_date.txt
OR
echo %date%>current_date.txt
Then pass the current date to the date command:
date<current_date.txt
You must take in account that US date is a bit different from europe date. Giving Date displayes:
>date
The current date is: 2019-09-12
Enter the new date: (yy-mm-dd)
But US date will be something like: mm-dd-yy
Better have a look at: https://www.robvanderwoude.com/batexamples.php
精彩评论