Dos command to get the current time

= = This is really frustrating me dead. . . .

Reference: http: //bbs.bathome.net/thread-3328-1-1.html

Operating systems, date format may be different: 

Tuesday 2008-07-29 
2008-07-29 Tue 
07/29/2008 Tue 
Tue 07/29/2008 

Taking into account other operating system language other than Chinese, English, the date format the species is too much. To standardize access to the current system date 20,080,728, the following two methods are not good: 
1. Direct use system variable% date% for string interception 
2. Direct results of the date command string interception 
actually registered by the date format table HKEY_CURRENT_USER \ value Control Panel \ International sShortDate inside to decide. 

Other methods to obtain the recommended system date: 

[Scheme 1] + PowerShell the BAT 
@echo OFF 
the REM Win7 / Win8 / Win10 systems comes PowerShell 
for / F %% I in ( 'PowerShell -C "-uformat the Get-a Date'% the Y %% m %% D% ' "') do ( 
    SET" Today = %% I " 
) 
echo,%% Today 
PAUSE 

for / F" delims = "%% I in ( 'PowerShell -C" the Get-a Date - UFormat '%% Y - %% m - %% d %% H: %% m: %% S' " '
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 
duplicated code 
[Scheme 4] + REG the BAT 
@echo OFF 
the REM is not guaranteed outside in British to give the correct result on the operating system, other languages 
for / F "delims =" %% a in ( 'Query REG "the HKEY_CURRENT_USER \ Control Panel \ International's" / V sShortDate') do ( 
    SET "RegDateOld = %% a" 
) 
SET RegDateOld RegDateOld% =: ~ -8% 
REG the Add "the HKEY_CURRENT_USER \ Control Panel \ International's" / sShortDate V / T REG_SZ / D-Md YYYY / F> NUL 
SET% = Today DATE:% = 
REG the Add "the HKEY_CURRENT_USER \ Control Panel \ International's "/ V sShortDate / REG_SZ T / D%% RegDateOld / F> NUL 
SET"Week = Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun " 
A %% in for (% Week%) do ( 
    Call SET "Today = %% Today: A = %% %%" 
) 
echo,Today%% 
PAUSE 
duplicated code
【方案5】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
复制代码
【方案6】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%% 
the REM needs to ensure registry editor is not in the locked state
@echo OFF
[Scheme 7] + REGEDIT the BAT
duplicated code
PAUSE
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
复制代码
【方案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 / AY = 0x %% A 
    SET MD = %% B 
) 
SET / AM% = 0x MD: ~, 2% 
SET / AD = 0x% MD: -2% ~ 
SET = 0 m m%% 
SET = 0 D % D% 
SET% = Y% Today -% m: ~ -2% -% D: -2% ~ 
echo,%% Today 
PAUSE 
duplicated code 
reproduced below in two acquisition date (CMD not a Windows system) inside the DOS system time code: 
original: http: //www.cn-dos.net/forum/viewthread.php tid = 19756? 
[original version] willsort 
@echo OFF 
IF "% 1" == "[(" GOTO the parse 
echo . | DATE | the Find "Current"> _getdate.bat 
echo the SET _dw %% = 3> current.bat 
echo the SET _date = %% 4 >> current.bat 
Call _getdate.bat 

echo |. DATE | the Find "the Enter"> _getdfmt .bat 
echo %%. 3 SET = _dfmt> 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%
NextToken GOTO 

: SetToken 
SET% = Tok. 4%. 6%. 5%. 9. 7%. 8% 
echo SET. 3% D1% =% >> _tmset.bat 
SET = D1 
GOTO End 

: End (End-of-File) 
copying the code

  

Guess you like

Origin www.cnblogs.com/zhizhiyin/p/11353670.html