批处理bat标准化获取当前系统日期的几种方法
程序员文章站
2022-06-24 16:10:38
操作系统不同,日期格式也可能不同:
星期二 2008-07-29
2008-07-29 星期二
07/29/2008 tue
tue 07/29/2008
&nb...
操作系统不同,日期格式也可能不同:
星期二 2008-07-29
2008-07-29 星期二
07/29/2008 tue
tue 07/29/2008
再考虑到中、英之外的其它语言的操作系统,日期格式的种类实在太多了。
要想标准化地获取当前系统日期2008-07-28,直接截取%date%变量的方法是不行的。
【方案一】bat + reg
@echo off rem 无法保证在中、英之外的其它语言的操作系统上得到正确结果 for /f "delims=" %%a in ('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 %%a in (%week%) do ( call set "today=%%today:%%a=%%" ) echo,%today% pause
【方案二】bat + reg
@echo off for /f "delims=" %%a in ('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
@echo off for /f "delims=" %%a in ('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 %%a in ('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
@echo off for /f "tokens=2 delims==" %%a in ('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
@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
【方案六】bat + regedit
@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
@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
上一篇: 快速解决ajax返回值给外部函数的问题
下一篇: ajax 乱码的一种解决方法