【方案一】BAT调用PowerShell- @echo off
- REM Win7/Win8/Win10等系统自带PowerShell
- REM 指定小时数
- set "HoursAgo=1"
-
- for /f "delims=" %%i in ('powershell -c "Get-Date (Get-Date).AddHours(-%HoursAgo%) -uformat '%%Y-%%m-%%d %%H:%%M:%%S'"') do (
- set "DstTime=%%i"
- )
- echo,%DstTime%
- pause
复制代码 【方案二】BAT调用VBS- @echo off
- REM 指定小时数
- set "HoursAgo=1"
-
- >"%temp%\MyDate.vbs" echo strLastHours=DateAdd("h", -%HoursAgo%, now)
- >>"%temp%\MyDate.vbs" echo strFmtDate=Right(Year(strLastHours),4) ^& Right("0" ^& Month(strLastHours),2) ^& Right("0" ^& Day(strLastHours),2) ^& Right("0" ^& Hour(strLastHours),2) ^& Right("0" ^& Minute(strLastHours),2) ^& Right("0" ^& Second(strLastHours),2)
- >>"%temp%\MyDate.vbs" echo WScript.Echo strFmtDate
- for /f "delims=" %%i in ('cscript /nologo "%temp%\MyDate.vbs"') do (
- set "DstTime=%%i"
- )
- set DstTime=%DstTime:~0,4%-%DstTime:~4,2%-%DstTime:~6,2% %DstTime:~8,2%:%DstTime:~10,2%:%DstTime:~12,2%
- echo,%DstTime%
- pause
复制代码 【方案三】BAT调用日期时间函数- @echo off
- REM 此类方法的关键在于如何获取标准化的当前系统日期
- REM 请参考http://bbs.bathome.cn/thread-3328-1-1.html
- REM 然后调用 Ritchie Lawrence 的日期函数,便可扩展出很多种方法,此处不再赘述。
- REM 日期转换的核心算法请参考http://bbs.bathome.cn/thread-3056-1-1.html
-
- REM 指定小时数
- set "HoursAgo=1"
-
- for /f "tokens=3" %%a in ('reg query "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate ^| findstr "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
- call :DateToSecs %date:~0,4% %date:~5,2% %date:~8,2% %time:~0,2% %time:~3,2% %time:~6,2% PassSeconds
- reg add "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate /t REG_SZ /d %RegDateOld% /f >nul
- set /a PassSeconds-=HoursAgo*60*60
- call :SecsToDate %PassSeconds% DstYear DstMonth DstDay DstHour DstMinute DstSecond
- set DstTime=%DstYear%-%DstMonth%-%DstDay% %DstHour%:%DstMinute%:%DstSecond%
- echo,%DstTime%
- pause
- goto :eof
-
- :DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
- setlocal ENABLEEXTENSIONS
- set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set ss=%6
- if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
- set /a dd=100%dd%%%100,mm=100%mm%%%100
- set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
- set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
- if 1%hh% LSS 20 set hh=0%hh%
- if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
- if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
- if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
- set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
- set /a j=j*86400+hh*3600+nn*60+ss
- endlocal&set %7=%j%&goto :EOF
-
- :SecsToDate %secs% yy mm dd hh nn ss
- setlocal ENABLEEXTENSIONS
- set /a i=%1,ss=i%%60,i/=60,nn=i%%60,i/=60,hh=i%%24,dd=i/24,i/=24
- set /a a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
- set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
- set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
- (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
- (if %hh% LSS 10 set hh=0%hh%)&(if %nn% LSS 10 set nn=0%nn%)
- if %ss% LSS 10 set ss=0%ss%
- endlocal&set %7=%ss%&set %6=%nn%&set %5=%hh%&^
- set %4=%dd%&set %3=%mm%&set %2=%yy%&goto :EOF
复制代码
|