操作系统不同,日期格式也可能不同:
星期二 2008-07-29
2008-07-29 星期二
07/29/2008 Tue
Tue 07/29/2008
再考虑到中、英之外的其它语言的操作系统,日期格式的种类实在太多了。
要想标准化地获取当前系统日期2008-07-28,直接截取%date%变量的方法是不行的。
【方案一】BAT + REG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@echo off
rem 无法保证在中、英之外的其它语言的操作系统上得到正确结果
for /f "delims=" %%ain ('reg query "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate')do (
set "RegDateOld=%%a"
)
set RegDateOld=%RegDateOld:~-8%
reg add"HKEY_CURRENT_USER/Control Panel/International" /v sShortDate/t REG_SZ/d yyyy-M-d/f>nul
set Today=%date: =%
reg add"HKEY_CURRENT_USER/Control Panel/International" /v sShortDate/t REG_SZ/d %RegDateOld%/f>nul
set "Week=Mon Tue Wed Thu Fri Sat Sun 星期一 星期二 星期三 星期四 星期五 星期六 星期日"
for %%ain (%Week%)do (
callset "Today=%%Today:%%a=%%"
)
echo,%Today%
pause
【方案二】BAT + REG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@echo off
for /f "delims=" %%ain ('reg query "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate')do (
set "RegDateOld=%%a"
)
set RegDateOld=%RegDateOld:~-8%
reg add"HKEY_CURRENT_USER/Control Panel/International" /v sShortDate/t REG_SZ/d yyyy-M-d/f>nul
set Today=%date: =%
reg add"HKEY_CURRENT_USER/Control Panel/International" /v sShortDate/t REG_SZ/d %RegDateOld%/f>nul
if "%Today:~0,1%" gtr"9" (
set Today=%Today:~-10%
)else (
set Today=%Today:~0,10%
)
echo,%Today%
pause
【方案三】BAT + REG
1
2
3
4
5
6
7
8
9
10
11
12
13
@echo off
for /f "delims=" %%ain ('reg query "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate')do (
set "RegDateOld=%%a"
)
set RegDateOld=%RegDateOld:~-8%
reg add"HKEY_CURRENT_USER/Control Panel/International" /v sShortDate/t REG_SZ/d yyyy-M-d/f>nul
type nul>"%temp%/MyFile.tmp"
for /f %%ain ('dir "%temp%/MyFile.tmp" ^| findstr /i "MyFile.tmp"')do (
set Today=%%a
)
reg add"HKEY_CURRENT_USER/Control Panel/International" /v sShortDate/t REG_SZ/d %RegDateOld%/f>nul
echo,%Today%
pause
【方案四】BAT + WMIC
1
2
3
4
5
6
7
@echo off
for /f "tokens=2 delims==" %%ain ('wmic path win32_operatingsystem get LocalDateTime /value')do (
set t=%%a
)
set Today=%t:~0,4%-%t:~4,2%-%t:~6,2%
echo,%Today%
pause
【方案五】BAT + VBS
1
2
3
4
5
6
7
8
9
@echo off
>"%temp%/DateCalculate.vbs" echo dt=date()
>>"%temp%/DateCalculate.vbs" echo s=right(year(dt),4) ^&"-" ^& right("0" ^& month(dt),2) ^&"-" ^& right("0" ^& day(dt),2)
>>"%temp%/DateCalculate.vbs" echo wscript.echo s
for /f %%ain ('cscript /nologo "%temp%/DateCalculate.vbs"')do set (
Today=%%a
)
echo,%Today%
pause
【方案六】BAT + REGEDIT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@echo off
rem 需要保证注册表编辑器没有处于锁定状态
regedit/e "%temp%/bak.reg" "HKEY_CURRENT_USER/Control Panel/International"
>"%temp%/new.reg" echo REGEDIT4
>>"%temp%/new.reg" echo,
>>"%temp%/new.reg" echo [HKEY_CURRENT_USER/Control Panel/International]
>>"%temp%/new.reg" echo "sShortDate"="yyyy-MM-dd"
regedit/s "%temp%/new.reg"
set Today=%date: =%
regedit/s "%temp%/bak.reg"
if "%Today:~0,1%" gtr"9" (
set Today=%Today:~-10%
)else (
set Today=%Today:~0,10%
)
echo,%Today%
pause
【方案七】BAT + DEBUG
1
2
3
4
5
6
7
8
9
10
11
12
@echo off
for /f "tokens=6,8 delims== " %%ain ('^(echo a100^&echo mov ah^,2a^&echo int 21^&echo.^&echo p 2^&echo q^)^|debug^|find "CX"')do (
set /a y=0x%%a
set md=%%b
)
set /a m=0x%md:~,2%
set /a d=0x%md:~-2%
set m=0%m%
set d=0%d%
set Today=%y%-%m:~-2%-%d:~-2%
echo,%Today%
pause
编写Windows批处理时经常会需要使用到日期和时间作为文件名,所以是非常重要的。
如何获取日期呢?
格式: %date%
结果: 2012-07-31
如何获取时间呢?
格式: %time%
结果: 10:21:21.68
这是获取的系统标准格式的日期和时间,但是有时候我们需要使用规定的格式,怎么办呢?
格式: %date:~x,y%以及%time:~x,y%
说明: x是开始位置,y是取得字符数
比如说我要获取完整的日期和时间,
格式: %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%
结果: 20120731111039
但是如果时间早于10点,那么只显示日期不显示时间,如:20120731,如何解决呢?
格式: %date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%
结果: 20120731052539
这样就达到我们的目标,成功的得到日期和时间了。