Bat 取得系统日期

取得日期的方式如下:

set yyyy=%date:~0,4%

yyyy是自己定义的变量名称,date是系统本身的变量,%date:~0,4% 的意思是从变量date的第0位开始,取得4位。

取得时间的方式相似:

set hh=%time:~0,2%

如果想要的小时是dd格式(例如:08:10:23)可以使用下面方法进行格式化:

set hh=%hh: =0%

这一句的意思是,把hh中的空格替换成0。

下面的代码功能是取得系统时间的前一个小时,是从百度知道里复制来的。

@echo off
setlocal enabledelayedexpansion
set a=%time:~,2%
set b=%date:~8,2%
set c=%date:~5,2%
set d=%date:~,4%
if %a%==0 (set a=23&& goto riq) else (set /a a-=1 && goto jes)
:riq
if %b%==01 (goto yue)
if %b%==08 (set b=07&& goto jes)
if %b%==09 (set b=08&& goto jes) else ( set /a b-=1 && goto jes)
:yue
if %c%==01 (set c=12& set b=31& set /a d-=1 && goto jes)
if %c%==03 (set /a g=%d%/4*4& if !g!==%d% (set b=29& set /a c-=1 && goto jes) else (set b=28& set /a c-=1 && goto jes))
if %c%==08 (set c=07& set b=31 && goto jes)
if %c%==09 (set c=08& set b=31 && goto jes)
echo 02>m
echo 04>>m
echo 06>>m
echo 11>>m
for /F %%i in ('findstr %c% m') do (set b=31& set /a c-=1&& goto jes)
set b=30& set /a c-=1 && goto jes
:jes
echo !d!-!c!-!b! !a!%time:~2,6%
pause
exit

猜你喜欢

转载自tjmljw.iteye.com/blog/2065808
bat