Computer Hope
Microsoft => Microsoft DOS => Topic started by: mat123 on March 31, 2010, 12:12:10 AM
-
how can i add one minute to the current time and store it as a variable
-
Start by advising the format of your system time as returned when time/t is entered.
-
again this topic, batch is never good dealing with time/date. Use vbscript
WScript.Echo DateAdd("n",1,Now)
or a good programming language eg Python
>>> import time,datetime
>>> t = datetime.datetime.now()
>>> t.ctime()
'Wed Mar 31 16:30:24 2010'
>>> back=datetime.datetime.fromtimestamp(time.mktime(t.timetuple())+60)
>>> back.ctime()
'Wed Mar 31 16:31:24 2010'
-
yes. as you can see python is clearly the winner here since it's four lines instead of 1.
Anyway for some reason I hardly ever use the DateAdd() function. Usually I just add the direct number equivalent (each whole numer is a day, an hour is 1/24, a minute is 1/24/60, etc. This is solely because I'm crazy. (Although I like to blame the fact that DateDiff was so filled with bugs for so many years and I just sort of grouped them all in the same department)
-
yes. as you can see python is clearly the winner here since it's four lines instead of 1.
lol. sarcasm? anyway, the datetime module in Python has much more features than what vbscript Date functions provides. when you see it this way, clearly Python is the winner. :)
-
lol. sarcasm? anyway, the datetime module in Python has much more features than what vbscript Date functions provides, if you would like to know.
And they probably weren't broken for 5+ years, either.
(that's totally frank, not sarcastic, btw)
There is probably a faster way to do it in python, also. In fact, everything you have there could be combined (I think, just want to exercise python here):
datetime.datetime.fromtimestamp(time.mktime(datetime.datetime.now().timetuple())+60).ctime()
Seems a tad verbose. Oh well.
Actually, VBScript is miles ahead of Batch but at the same time it's date support really blows, solely because it's all handled in OLEAUT32. Now, it works for your everyday date and time calculations but it leaves out important and useful things like converting between time zones and or calculating UTC times, something that from a quick look at the documentation it appears that python probably does.
That being said the date and time support in the .NET framework is "fixed" and actually works properly with UTC dates and time zone information. It's a very rich framework much like the modules included with Python. For example, in powershell, the following would display the current time plus one minute.
[System.DateTime]::Now.AddMinutes(1)
-
There is probably a faster way to do it in python, also. In fact, everything you have there could be combined (I think, just want to exercise python here):
sure, that's the beauty of objects. I prefer to break it down to intermediate steps for readability though
Actually, VBScript is miles ahead of Batch but at the same time it's date support really blows, solely because it's all handled in OLEAUT32. Now, it works for your everyday date and time calculations but it leaves out important and useful things like converting between time zones and or calculating UTC times, something that from a quick look at the documentation it appears that python probably does.
precisely. not just Python, Perl as well has very good featured datetime libraries for advance date ops
That being said the date and time support in the .NET framework is "fixed" and actually works properly with UTC dates and time zone information. It's a very rich framework much like the modules included with Python. For example, in powershell, the following would display the current time plus one minute.
i agree. powershell is probably the way to go for modern windows scripting.
-
can any of the codes store the new time in dos.
-
Can any of the codes store the new time in batch.
Yes the following batch code assigns minute plus 1 to variable MM1:
C:\batch>type addminute.bat
@echo OFF
REM time format 12:46:26.94
echo start time=%TIME%
set HH=%TIME:~0,2%
echo Hour=%HH%
set MM=%TIME:~3,2%
echo minutes=%MM%
set /a MM1=%MM% + 1
echo minutes plus 1=%MM1%
Output:
C:\batch> addminute.bat
start time=17:27:13.98
Hour=17
minutes=27
minutes plus 1=28
C:\batch>
-
Not to be too obvious, but I think you need a plan B:
C:\batch> addminute.bat
start time=17:27:13.98
Hour=17
minutes=27
minutes plus 1=28
C:\batch>
What happens if the minutes value is 59?
Just asking. 8)
-
Not to be too obvious, but I think you need a plan B:
What happens if the minutes value is 59?
Just asking. 8)
C:\batch> addminute.bat
start time=19:59:35.10
Hour=18
minutes=59
minutes plus 1=60
C:\batch>
-
Yes the following batch code assigns minute plus 1 to variable MM1:
you cannot just add your minute like that. after 59, it should be 00, not 60 and then the hour incremented. you will need a more elaborate method.
-
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
d:\batch>WScript.Echo DateAdd("n",1,Now)
'WScript.Echo' is not recognized as an internal or external command,
operable program or batch file.
d:\batch>
Did I miss something?
EDIT:
IF minutes =59 ????
IF hours = 23 ????
IF days = 28 and month = February ??
IF mod(year/4) = 0 ???
IF you are on a ship and...
about to cross the international date line in 30 seconds ::) ::)
-
Did I miss something?
yes. that's a vbscript statement. so you cannot run it like that. you put that in a file and then run it using cscript.exe
cscript //nologo mydateadd.vbs
-
Not to be too obvious, but I think you need a plan B:
What happens if the minutes value is 59?
C:\batch>type addplanB.bat
@echo OFF
REM time format 12:46:26.94
echo start time=%TIME%
set HH=%TIME:~0,2%
echo Hour=%HH%
set MM=%TIME:~3,2%
if %MM%==59 Goto adjust
echo minutes=%MM%
set /a MM1=%MM% + 1
echo minutes plus 1=%MM1%
if %MM1%==60 goto adjust
goto end
:adjust
set /a MM1=0
echo MM1=%MM1%
Pause
set /a HH=%HH% + 1
echo minutes plus 1=%MM1%
echo Hour=%HH%
:end
Output:
C:\batch> add2.bat
start time=19:59:31.87
Hour=19
MM1=0
Press any key to continue . . .
minutes plus 1=0
Hour=20
C:\batch>
C:\batch> add2.bat
start time=20:07:14.17
Hour=20
minutes=07
minutes plus 1=8
C:\batch>
-
C:\batch> add2.bat
start time=19:59:31.87
Hour=19
MM1=0
Press any key to continue . . .
minutes plus 1=0
Hour=20
C:\batch>
End time=20:0:31.87?
-
C:\batch> add2.bat
start time=20:07:14.17
Hour=20
minutes=07
minutes plus 1=8
C:\batch>
how about hours? after 24 comes 00.
-
how about hours? after 24 comes 00.
No. There is no 24.
The clock goes from 00:00 to 23:59
But in 12 hour notation it goes from
12:00 AM to 11:59 PM
Ane then from
12:00 PM to 11:59 PM
-
hah, yes you are right. its 23:59
-
How about hours? after 24 comes 00.
Output:
C:\batch>add3.bat
start time=23:59:20.59
Hour=23
MM1=0
HH=0
C:\batch>type add3.bat
@echo OFF
REM time format 12:46:26.94
echo start time=%TIME%
set HH=%TIME:~0,2%
echo Hour=%HH%
set MM=%TIME:~3,2%
if %MM%==59 Goto adjust
echo minutes=%MM%
set /a MM1=%MM% + 1
echo minutes plus 1=%MM1%
if %MM1%==60 goto adjust
goto end
:adjust
set /a MM1=0
echo MM1=%MM1%
If %HH%==23 goto end
set /a HH=%HH% + 1
echo minutes plus 1=%MM1%
echo Hour=%HH%
:end
If %HH%==23 set /a HH=0
echo HH=%HH%
-
using your new code and setting TIME variable to 23:58.59.00 gives me
C:\test>test.bat
start time=23:58:59.00
Hour=23
minutes=58
minutes plus 1=59
HH=0
if HH is the final answer for hours, then 0 in the above result is incorrect.
-
How can I add one minute to the current time and store it as a variable
The batch solution is the best solution. Matt123 ask for the minutes plus
one minute be assigned to a "Dos" variable. This has been accomplished.
The 59 minute bug and the 23 Hour bug has been corrected.
I will not write any code for a 12 hour clock.
QED
p.s. "Sometime we cannot see the forest; we can only see the trees."
-
The batch solution is the best solution.
yeah right. in a million years
Matt123 ask for the minutes plus
one minute be assigned to a "Dos" variable. This has been accomplished.
no, it has not. you won't know what time format his PC is beforehand, so you have to take care of that.
-
using your new code and setting TIME variable to 23:58.59.00 gives me
if HH is the final answer for hours, then 0 in the above result is incorrect.
Thanks for finding the minor bugs. Code written here in few minutes is not
production code. We are not being paid.
p.s. Ghostdog74, I have not seen any output of the code you wrote for this thread.
I believe the Batch code is the best solution. Matt, the Original Poster, requested a batch solution. I have not seen Ghostdog74's batch solution.
-
Greg, I hate to take the wind out of your sails, but batch is terrible at date manipulation. VBS isn't the BEST, but it is better than what pure batch can do.
-
This is a lot of FUN!
Is anybody taking this seriously? 8)
Professional administrators have done this sort of thing years ago. You can manipulate the date in batch scripts without the need to use VBscript or something like that. The trick is to understand the conversion to Julian date. The DOS DATE command does not do Julian date number, but you can convert it and do some add or subtract and come up with an answer. But you need to define what a 'month' is. Often when people say 'in a month', they mean exactly 30 days.
Here is how you do Julian date in DOS. Read it if you dare.
http://www.robvanderwoude.com/datetimentmath.php
-
Professional administrators have done this sort of thing years ago. You can manipulate the date in batch scripts without the need to use VBscript or something like that. The trick is to understand the conversion to Julian date. The DOS DATE command does not do Julian date number, but you can convert it and do some add or subtract and come up with an answer.
So you need to convert to a julian date, do the addition, and then convert back. just leverage both batch and VBScript at once.
C:\Users\BC_Programming>for /f "tokens=*" %P in ('cscript /NOLOGO temp.vbs') do
set newdate=%P
@echo off
echo WScript.echo(time+(1/24/60)) >> temp.vbs&&for /f "tokens=*" %%P in ('Cscript /NOLOGO temp.vbs') do set newdate=%%P
del temp.vbs
after running this, the newdate variable will have the current time plus a minute. Note there isn't a huge advantage between this solution and gregs; the only difference is this one can easily be told to use the entire date by changing time to now, whereas the pure batch solution will then need to have a lot of checks for overflows to the next date,month, and year.
But you need to define what a 'month' is. Often when people say 'in a month', they mean exactly 30 days.
Er... No. They almost always mean the same date in the following month. Depends on the context, though.
For example, if it's the 12th, and somebody says, "in a month" they might mean the 12th of the following month, or they might mean 30.
I think the most important thing here is that we are adding minutes, so it really won't matter how long a month is; it might if we were considering a complete Date/Time, but the OP only said the current time... so we go with that.
-
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
d:\batch>WScript.Echo DateAdd("n",1,Now)
'WScript.Echo' is not recognized as an internal or external command,
operable program or batch file.
d:\batch>
yes. that's a vbscript statement. so you cannot run it like that. you put that in a file and then run it using cscript.exe
cscript //nologo mydateadd.vbs
No need for a script file. You can run from the command prompt:
mshta vbscript:(MsgBox(DateAdd("n",1,Now)))
No. There is no 24.
The clock goes from 00:00 to 23:59
But in 12 hour notation it goes from
12:00 AM to 11:59 PM
Ane then from
12:00 PM to 11:59 PM
In the US military there is a twenty-four hundred hours, however the first tick past midnight becomes zero hundred hours.
Just thought I'd throw that in. 8)
-
No need for a script file. You can run from the command prompt:
Except that using that method there is no way to get the output directly back into a environment variable.
-
mshta vbscript:(MsgBox(DateAdd("n",1,Now)))
for this example maybe. but if its part of a larger program, then maybe not. a script/file is still needed.
-
No need for a script file. You can run from the command prompt:
mshta vbscript:(MsgBox(DateAdd("n",1,Now)))
How do we save the output of
b]mshta vbscript:(MsgBox(DateAdd("n",1,Now)))[/b]
to a batch variable? Matt, the original poster, ask that the output be assigned to
batch variable.
C:\batch>time /t
12:15 PM
C:\batch>mshta vbscript:(MsgBox(DateAdd("n",1,Now)))
C:\batch>
(http://i7.photobucket.com/albums/y268/billrich/SW01.jpg)
(http://i7.photobucket.com/albums/y268/billrich/SW01.jpg)
[recovering disk space - old attachment deleted by admin]
-
Normally I try not to get involved with all the shenanigans that seem to go on, but sometimes these threads get turned around into Greg's questions and the OP seems forgotten.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
d:\batch>WScript.Echo DateAdd("n",1,Now)
I posted the mshta one-liner in response to Geek-9PM. It was merely to demonstrate how to run VBScript instructions from the command prompt.
How do we save the output of
mshta vbscript:(MsgBox(DateAdd("n",1,Now)))
to a batch variable? Matt, the original poster, ask that the output be assigned to
batch variable.
I thought you had given the definitive answer with your batch solution Greg. Far be it from me to steal your thunder. I'll leave the OP in your capable hands to answer the age old question "How do we save the output of mshta vbscript:(MsgBox(DateAdd("n",1,Now))) to a batch variable? I'm actually looking forward to the answer myself.
Good luck. 8)
-
I'm actually looking forward to the answer myself.
Good luck. 8)
for /f "delims=" %%a in ('mshta vbscript:(MsgBox(DateAdd("n",1,Now)))') do set newtime=%%a
Now, I got a really strong feeling that that won't work...
-
for /f "delims=" %%a in ('mshta vbscript:(MsgBox(DateAdd("n",1,Now)))') do set newtime=%%a
Now, I got a really strong feeling that that won't work...
Nope. the issue here is that mshta doesn't use the console, certainly not if you use Msgbox. In order to get mshta to output to the console, one would need to write a COM component that attaches to the console and outputs a string to it, create it with CreateObject() and call that method with the calculation. All a bit too much work for what is necessary in this circumstance.
-
I posted the mshta one-liner in response to Geek-9PM. It was merely to demonstrate how to run VBScript instructions from the command prompt.
"How do we save the output of mshta vbscript:(MsgBox(DateAdd("n",1,Now))) to a batch variable? I'm actually looking forward to the answer myself.
I don't know the answer and I don't know how to find it. I have never used VBS.
mshta vbscript:(MsgBox(DateAdd("n",1,Now))) works very well much better than the batch file.
It also works without MsgBox().
mshta vbscript:(DateAdd("n",1,Now)) works but I cannot assign the output to a variable nor redirect the output.
Thanks for your help.
I suspect you have already furnished the OP with the answer he needs.
-
greg your batch solution worked
-
your thunder
Damp firecracker more like.
-
Damper.
Matt wrote:
"Greg, your batch solution worked."
-
Matt wrote:
"Greg, your batch solution worked."
Very good!
Do you remember how many sides a Triangle has too?
-
Very good!
Do you remember how many sides a Triangle has too?
A Triangle has four sides and 360 degrees.
-
A Triangle has four side and 360 degrees.
I certainly hope that is a attempt at humour.
-
I certainly hope that is a attempt at humour.
A Triangle has four sides and 360 degrees.
Those are facts.
But back on topic:
mshta vbscript:(DateAdd("n",1,Now)) works but I cannot assign the output to a variable nor redirect the output.
(http://i7.photobucket.com/albums/y268/billrich/dateadd.jpg)
-
A Triangle has four sides and 360 degrees.
Those are facts.
Greg, the Pythagoras de nos jours.
-
How can I add one minute to the current time and store it as a variable.
Here is another solution. Half VBS and half batch.
BC_programmer wrote the VBS half.
C:\batch>type bc4.bat
@echo off
rem bc4.vbs written by BC_programmer
rem curDate=Now
rem WScript.echo "Current Date:" + Cstr(curDate)
rem newDate = dateAdd("n",1,Now)
rem WScript.echo "Date plus 1 minute" + Cstr(newdate)
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('cscript //nologo bc4.vbs') do (
rem echo %%i
set batvar="%%i"
echo.
echo batvar=!batvar!
)
Output:
C:\batch>bc4.bat
batvar="Current Date:4/2/2010 7:51:04 PM"
batvar="Date plus 1 minute:4/2/2010 7:52:04 PM"
C:\batch>
-
Greg, a triangle has 3 sides, each vertex adding up to 180 degrees.
greg your batch solution worked
I assume the topic can be closed now?
-
I assume the topic can be closed now?
I was not aware that only one solution to the OP's question
was allowed in a thread?
We are not able to delete our posts after certain amount of time?
p.s. : The off topic triangle information was introducted
by BC_Programmer. It is difficult to remain on topic.
Everyone knows the definition of triangle.
-
p.s. : The off topic triangle information was introducted
by BC_Programmer. It is difficult to remain on topic.
Everyone knows the definition of triangle.
Were you joking before about triangles having four sides? Only it's hard to tell.
-
Where is Salmon Trout's solution for the OP's question?
"How can I add one minute to the current time and store it as a variable?"
-
Where are Geg's meds? It sounds like it's that time of day.
-
There we go.