分钟时间批量添加(使用算术) [英] Add minutes to time in batch (using arithmetic)

查看:151
本文介绍了分钟时间批量添加(使用算术)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在陷入两难境地。我想送一个preemptive电子邮件给用户,告诉他们即将到来的任务是即将发生。为了做到这一点,我定义一些变量,通过BLAT发送电子邮件,睡在批次5分钟,然后执行脚本的其余部分。

I'm running into a dilemma. I want to send a preemptive email to users telling them that an upcoming task is about to happen. In order to do that I'm defining some variables, sending an email via blat, sleeping the batch for 5 minutes, then executing the rest of the script.

在执行%TIME%在下午4:00,我得到16:00:00.00。如果我在电子邮件中5分钟内加入到它,只为显示目的有以下code:

When executing %time% at 4:00PM, I get 16:00:00.00. If I add 5 minutes to it, only for display purposes in the email with the following code:

@echo on
SET /a timeminute = 00 + 5 << --- test code
::SET /a timeminute = %time:~3,2% + 5 << --- actual code in GoLive
IF %timeminute% LEQ 9 (
    GOTO :resetTime
) ELSE (
    GOTO :end
)
:resetTime
SET timeminute = "0%timeminute%"

:end
echo %timeminute%
pause

我得到5,不要像05预期。上一次使用算术下降前导零,所以我尽量在以后添加它放回,但后来SET是IF语句中,不能看到?我怎么能看到?有没有在批处理环境变量一个这样的东西?

I get 5, not 05 like expected. Using arithmetic on time drops leading zeros, so I try to add it back in later but the later SET is within the IF statement and cannot be seen? How can I see that? Is there a such thing as an environment variable in batch?

请记住,这只是问题的小时前9分钟之内发生,该时间之后,有没有更多的前导零。

Keep in mind this issue only happens within the first 9 minute of the hour, after that time, there are no more leading zeros.

奖励:当在一个小时内分钟数是55-59,会发生什么?在我的例子,这将是60-64岁,所以我需要围捕一个小时的路,走剩余分钟的照顾。现在,我认为这是一个错误,但我不预见这个脚本那些奇怪的时间运行。但如果是一个简单的办法,请让我知道,因为我还没有尝试过,甚至解决这一问题。

Bonus: What happens when the minutes in a hour is 55-59? In my example, it will be 60-64, so I need a way of rounding up an hour and take care of the remaining minutes. Right now, I see that as a bug, but I do not foresee this script running at those odd times. But if it is an easy fix please let me know as I have not even tried to tackle that problem.

太感谢您了。

推荐答案

一个更紧凑的形式做同样事情是这样的:

A more compact form to do the same thing is this:

@echo on
for /F "tokens=1-3 delims=:." %%a in ("%time%") do (
   set timeHour=%%a
   set timeMinute=%%b
   set timeSeconds=%%c
)
rem Convert HH:MM to minutes + 5
set /A newTime=timeHour*60 + timeMinute + 5
rem Convert new time back to HH:MM
set /A timeHour=newTime/60, timeMinute=newTime%%60
rem Adjust new hour and minute
if %timeHour% gtr 23 set timeHour=0
if %timeHour% lss 10 set timeHour=0%timeHour%
if %timeMinute% lss 10 set timeMinute=0%timeMinute%
echo %timeHour%:%timeMinute%:%timeSeconds%
pause

这篇关于分钟时间批量添加(使用算术)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆