= = 这个真的折腾死我了。。。。
参考:http://bbs.bathome.net/thread-3328-1-1.html
操作系统不同,日期格式也可能不同: 星期二 2008-07-29 2008-07-29 星期二 07/29/2008 Tue Tue 07/29/2008 再考虑到中、英之外的其它语言的操作系统,日期格式的种类实在太多了。要想标准化地获取当前系统日期20080728,以下两个方法都不好: 1. 直接使用系统变量 %date% 进行字符串截取 2. 直接对 date 命令的结果进行字符串截取 实际上日期格式是由注册表 HKEY_CURRENT_USERControl PanelInternational 里面 sShortDate 的值来决定的。 推荐使用其它方法来获取系统日期: 【方案1】BAT + PowerShell @echo off REM Win7/Win8/Win10等系统自带PowerShell for /f %%i in ('powershell -c "Get-Date -uformat '%%Y%%m%%d'"') do ( set "Today=%%i" ) echo,%Today% pause for /f "delims=" %%i in ('powershell -c "Get-Date -UFormat '%%Y-%%m-%%d %%H:%%M:%%S'"') do ( set "SysTS=%%i" ) echo %SysTS% pause 复制代码 【方案2】BAT + WMIC @echo off for /f "tokens=2 delims==" %%i in ('wmic path win32_operatingsystem get LocalDateTime /value ^| findstr "="') do ( set "strDate=%%i" ) set "Today=%strDate:~0,4%%strDate:~4,2%%strDate:~6,2%" echo,%Today% pause set "Now=%strDate:~0,4%-%strDate:~4,2%-%strDate:~6,2% %strDate:~8,2%:%strDate:~10,2%:%strDate:~12,2%" echo %Now% pause 复制代码 【方案3】BAT + VBS @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 %%a in ('cscript /nologo "%temp%DateCalculate.vbs"') do ( set Today=%%a ) echo,%Today% pause 复制代码 【方案4】BAT + REG @echo off REM 无法保证在中、英之外的其它语言的操作系统上得到正确结果 for /f "delims=" %%a in ('reg query "HKEY_CURRENT_USERControl PanelInternational" /v sShortDate') do ( set "RegDateOld=%%a" ) set RegDateOld=%RegDateOld:~-8% reg add "HKEY_CURRENT_USERControl PanelInternational" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul set Today=%date: =% reg add "HKEY_CURRENT_USERControl PanelInternational" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul set "Week=Mon Tue Wed Thu Fri Sat Sun 星期一 星期二 星期三 星期四 星期五 星期六 星期日" for %%a in (%Week%) do ( call set "Today=%%Today:%%a=%%" ) echo,%Today% pause 复制代码 【方案5】BAT + REG @echo off for /f "delims=" %%a in ('reg query "HKEY_CURRENT_USERControl PanelInternational" /v sShortDate') do ( set "RegDateOld=%%a" ) set RegDateOld=%RegDateOld:~-8% reg add "HKEY_CURRENT_USERControl PanelInternational" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul set Today=%date: =% reg add "HKEY_CURRENT_USERControl PanelInternational" /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 复制代码 【方案6】BAT + REG @echo off for /f "delims=" %%a in ('reg query "HKEY_CURRENT_USERControl PanelInternational" /v sShortDate') do ( set "RegDateOld=%%a" ) set RegDateOld=%RegDateOld:~-8% reg add "HKEY_CURRENT_USERControl PanelInternational" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul type nul>"%temp%MyFile.tmp" for /f %%a in ('dir "%temp%MyFile.tmp" ^| findstr /i "MyFile.tmp"') do ( set Today=%%a ) reg add "HKEY_CURRENT_USERControl PanelInternational" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul echo,%Today% pause 复制代码 【方案7】BAT + REGEDIT @echo off REM 需要保证注册表编辑器没有处于锁定状态 regedit /e "%temp%ak.reg" "HKEY_CURRENT_USERControl PanelInternational" >"%temp% ew.reg" echo REGEDIT4 >>"%temp% ew.reg" echo, >>"%temp% ew.reg" echo [HKEY_CURRENT_USERControl PanelInternational] >>"%temp% ew.reg" echo "sShortDate"="yyyy-MM-dd" regedit /s "%temp% ew.reg" set Today=%date: =% regedit /s "%temp%ak.reg" if "%Today:~0,1%" gtr "9" ( set Today=%Today:~-10% ) else ( set Today=%Today:~0,10% ) echo,%Today% pause 复制代码 【方案8】BAT + DEBUG @echo off for /f "tokens=6,8 delims== " %%a in ('^(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 复制代码 以下转载两个在DOS系统(不是Windows系统的CMD)里面获取日期、时间的代码: 原文:http://www.cn-dos.net/forum/viewthread.php?tid=19756 【willsort原始版】 @echo off if "%1"=="[(" goto parse echo.|date|find "Current" > _getdate.bat echo set _dw=%%3> current.bat echo set _date=%%4>> current.bat call _getdate.bat echo.|date|find "Enter" > _getdfmt.bat echo set _dfmt=%%3> enter.bat call _getdfmt.bat echo.|time|find "Current" > _gettime.bat echo set _time=%%3> current.bat call _gettime.bat echo _|choice /c%_dfmt%%_date%_%_time%_ @%0 > _parse.bat %comspec% /e:2048 /c _parse.bat goto end :parse set _dt1=_%2%3 set _dt2=_%5%6 set _dt3=_%8%9 for %%t in ([,Y,Y,-,M,M,-,D,D,]) do shift set _vdt= :loop1 shift set _tmp=%_vdt% for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1 if not "%_vdt%"=="%_tmp%" goto loop1 set %_dt1%=%_vdt% set _vdt= :loop2 shift set _tmp=%_vdt% for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1 if not "%_vdt%"=="%_tmp%" goto loop2 set %_dt2%=%_vdt% set _vdt= :loop3 shift set _tmp=%_vdt% for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1 if not "%_vdt%"=="%_tmp%" goto loop3 set %_dt3%=%_vdt% set _vdt= :loop4 shift set _tmp=%_vdt% for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1 if not "%_vdt%"=="%_tmp%" goto loop4 set _HH=%_vdt% set _vdt= :loop5 shift set _tmp=%_vdt% for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1 if not "%_vdt%"=="%_tmp%" goto loop5 set _MN=%_vdt% set _vdt= :loop6 shift set _tmp=%_vdt% for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1 if not "%_vdt%"=="%_tmp%" goto loop6 set _SS=%_vdt% set _vdt= :loop7 shift set _tmp=%_vdt% for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1 if not "%_vdt%"=="%_tmp%" goto loop7 set _CS=%_vdt% echo DTN:%_YY% %_MM% %_DD% %_HH% %_MN% %_SS% %_CS% :end 复制代码 【willsort简化版】 @echo off if "%1"==":" goto %2 (subroutine handler at %2) echo.exit|%comspec%/e:4096/k prompt set v=$d $t$_:>_tm.bat call _tm.bat echo.%v%&|%comspec%/e:4096/c %0 : Parse>_tm.bat for %%p in (_tm.bat* _tmset.bat*) do call %%p echo.Note that d1 d2 and d3 are in your system date order echo.DTN:d1=%d1% d2=%d2% d3=%d3% hh=%hh% mm=%mm% ss=%ss% cs=%cs%. for %%e in (v d1 d2 d3 hh mm ss cs) do set %%e= for %%f in (_tm.bat* _tm.tmp* _tmset.bat*) do del %%f goto end :Parse (usage: call %0 : Parse +input redirect date/time) set me=%0 set tok=d1 d2 d3 hh mm ss cs set d1= :NextToken for %%v in (v1 v) do set %%v= choice /c&0123456789 /n>_tm.tmp for %%n in (0 1 2 3 4 5 6 7 8 9) do if errorlevel 77%%n set v=%%n if not errorlevel 2 %me% : SetToken %tok% find ""<_tm.tmp>nul if not errorlevel 1 if not "%d1%"=="" call %me% : SetToken %tok% set d1=%d1%%v% goto NextToken :SetToken set tok=%4 %5 %6 %7 %8 %9 echo set %3=%d1%>> _tmset.bat set d1= goto end :end (end-of-file) 复制代码