转自http://blog.csdn.net/iw1210/article/details/39313677
下面介绍在Windows批处理文件中引用日期和时间的方法。
%DATE%输出的是: yyyy/mm/dd 星期* (例如:2008/12/18 星期四)
引用格式:%DATE:~x,y%
x=起始坐标,y=从x开始截取字符个数
如:
%DATE:~0,4% --> yyyy
%DATE:~5,2% --> mm
%DATE:~8,2% --> dd
如取出格式为 YYYYMMDD 的日期:%DATE:~0,4%%DATE:~5,2%%DATE:~8,2% -->20081218
%TIME%输出的是:hh24:mi:ss.ms (例如:10:49:12.17)
引用格式:%TIME:~x,y%
x=起始坐标,y=从x开始截取字符个数
如:
%TIME:~0,2% --> hh
%TIME:~3,2% --> mi
%TIME:~6,2% --> ss
如取出格式为 HHMMSS 的时间:%TIME:~0,2%%TIME:~3,2%%TIME:~6,2% -->104912
输出格式化后的日期时间:%DATE:~4,4%%DATE:~9,2%%DATE:~12,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2% -->20081218104912
举例说明,把下面内容复制到一个.bat 文件保存运行。
- @echo off
- echo *** %DATE%
- echo *** %TIME%
- set THISDATE=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
- echo %THISDATE%
- set THISTIME=%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
- echo %THISTIME%
- set THISDATETIME=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
- echo %THISDATETIME%
- pause
运行结果:
*** 2014-09-16 星期二
*** 10:26:46.43
20140916
102646
20140916102646
请按任意键继续. . .