码迷,mamicode.com
首页 > 其他好文 > 详细

bat 时间 的运算与提取

时间:2016-07-31 17:46:27      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

比如在系统中date这个环境变量的值为
2011-07-23 星期六
年------%date:~0,4%      表示从左向右指针向右偏0位,然后从指针偏移到的位置开始提取4位字符,结果是2011
月------%date:~5,2%        表示指针从左向右偏移5位,然后从偏移处开始提取2位字符,结果是07
日------%date:~8,2%                         8                                  31
%date:~5% 表示指针从左向右偏移5位,然后提取所有的值 时间-----%date:~-5% 表示指针反方向偏移,从最右端开始,偏移5位,然后从指针处提取左边的所有数值。 md F:\backup\%date:~0,4%%date:~5,2%%time:~0,2%%time:~3,2% 结果为:md F:\201107231855



echo.%date:~0%    日期date
echo.%time:~0%      时间time
echo. %date:~0,4%   年
echo.%date:~5,2%    月
echo.%date:~8,2%    日
echo.%time:~0,2%   小时
echo.%time:~3,2%    分
echo.%time:~6,2%    秒
echo.%time:~9,2%   里秒

pause

set /a 指定等号右边的字符串为被评估的数字表达式。

就是表示计算



@echo off
set/a xz=123+456
echo %xz%
pause>nul

可以输出123+456的结果

 

时间运算

@echo off 
:: 判断一个脚本执行完毕所需要的时间 
:: 先取开始时间,然后在即将结束的时候取结束时间 
:: 两个时间都分别提取小时、分和秒数 
:: 分别对小时数、分钟数和秒数进行操作 
:: 还要对08和09这两个数进行操作 
:: 注意:set /a num=的格式只能处理两位数中是否高位为0,如果是多位数 
:: 要去高位的所有0的话,要用循环测试高位是否为0或者在高位添1然后 
:: 再减去1000之类的数字的方法 
:: code by JM 2006-9-510 CMD@XP 感谢pengfei测试 
set time_begin=%time:~0,-3% 
echo 脚本开始运行时间是 %time_begin% 
:: 小于10的小时数前有空格,要做去空格操作 
for /f "tokens=1,2,3 delims=:" %%i in ("%time_begin%") do ( 
set /a hour_b=%%i 
set /a munite_b=%%j 
set /a second_b=%%k 
) 
pause 
set time_end=%time:~0,-3% 
for /f "tokens=1,2,3 delims=:" %%i in ("%time_end%") do ( 
set /a hour_e=%%i 
set /a munite_e=%%j 
set /a second_e=%%k 
) 
call :time_lapse 
echo 脚本结束运行的时间是 %time_end% 
echo 共花费了 %hour_% 小时 %munite_% 分 %second_% 秒 
pause>nul 
goto :eof 
:time_lapse 
:: 一定要按照 秒=>分钟=>小时 的顺序操作 
if %second_e% lss %second_b% ( 
set /a munite_e=%munite_e%-1 
set /a second_e=%second_e%+60 
) 
set /a second_=%second_e%-%second_b% 
if %munite_e% lss %munite_b% ( 
set /a hour_e=%hour_e%-1 
set /a munite_e=%munite_e%+60 
) 
set /a munite_=%munite_e%-%munite_b% 
if %hour_e% lss %hour_b% ( 
set /a hour_e=%hour_e%+24 
) 
set /a hour_=%hour_e%-%hour_b% 
goto :eof 
另外一种方法(Code by Pengfei): 
@echo off 
::11:08:25.45 
:: 运行程序的时间统计 
set _time_start=%time% 
set /a hour_start=%_time_start:~0,2% 
set /a minute_start=1%_time_start:~3,2%-100 
set /a second_start=1%_time_start:~6,2%-100 
echo %time% 
echo %hour_start% 
echo %minute_start% 
echo %second_start% 
pause 
:: 结束程序的时间统计 
set _time_end=%time% 
set /a hour_end=%_time_end:~0,2% 
set /a minute_end=1%_time_end:~3,2%-100 
set /a second_end=1%_time_end:~6,2%-100 
echo %time% 
echo %hour_end% 
echo %minute_end% 
echo %second_end% 
pause 
:: 计算秒数 
if %second_end% lss %second_start% ( 
set /a second_end=%second_end%+60 
set /a minute_end=%minute_end%-1 
) 
set /a second=%second_end%-%second_start% 
:: 计算分钟数 
if %minute_end% lss %minute_start% ( 
set /a minute_end=%minute_end%+60 
set /a hour_end=%hour_end%-1 
) 
set /a minute=%minute_end%-%minute_start% 
:: 计算小时数 
if %hour_end% lss %hour_start% ( 
set /a hour_end=%hour_end%+24 
) 
set /a hour=%hour_end%-%hour_start% 
echo %hour%:%minute%:%second% 
pause 

 

bat 时间 的运算与提取

标签:

原文地址:http://www.cnblogs.com/--3q/p/5723277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!