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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值