批处理bat文件能不能得到随机时间和日期?

来源:百度知道 编辑:UC知道 时间:2024/06/05 09:19:19
批处理bat文件能不能得到随机时间和日期?批处理bat文件能不能得到随机时间和日期?
我是想获得时间
例如现在是2008年3月18日
我想随机的获得时间 例如 2006年5月2日 2007年3月2日
递增的。

@echo off
set /a years=%random:~0,4%
:lp1
set /a months=%random:~0,2%
if %months% gtr 12 goto lp1
:lp2
set /a days=%random:~0,2%
if %days% gtr 30 goto lp2
echo %years%年%months%月%days%日
pause

::至于要考虑31号、润月什么的再联系吧

@echo off
set AFile=bak-%DATE:~4,4%%DATE:~9,2%%DATE:~12,2%
set BFile=bak-%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
echo AFile=%AFile%.rar
echo BFile=%BFile%.rar

运行此批处理的结果:
AFile=bak-20060109.rar ------- 年月日 -- 8位
BFile=bak-140650.rar ---------- 时分秒 -- 6位

另:如果小时数只有一位数字,造成中间有空格而出错的问题,请使用如下方法补0
set hh=%time:~0,2%
if /i %hh% LSS 10 (set hh=0%time:~1,1%)