Computer Hope

Microsoft => Microsoft DOS => Topic started by: mat123 on March 31, 2010, 12:12:10 AM

Title: time
Post 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
Title: Re: time
Post by: T.C. on March 31, 2010, 01:12:47 AM
Start by advising the format of your system time as returned when time/t is entered.
Title: Re: time
Post by: ghostdog74 on March 31, 2010, 03:24:21 AM
again this topic, batch is never good dealing with time/date. Use vbscript

Code: [Select]
WScript.Echo DateAdd("n",1,Now)
or a good programming language eg Python

Code: [Select]
>>> 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'

Title: Re: time
Post by: BC_Programmer on March 31, 2010, 06:44:22 AM
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)
Title: Re: time
Post by: ghostdog74 on March 31, 2010, 06:54:38 AM
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. :)
Title: Re: time
Post by: BC_Programmer on March 31, 2010, 07:08:37 AM
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):
Code: [Select]
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.

Code: [Select]
[System.DateTime]::Now.AddMinutes(1)



Title: Re: time
Post by: ghostdog74 on March 31, 2010, 07:36:12 AM
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

Quote
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

Quote
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.
Title: Re: time
Post by: mat123 on March 31, 2010, 08:57:57 AM
can any of the codes store the new time in dos.
Title: Re: time
Post by: greg on March 31, 2010, 04:28:17 PM
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
Code: [Select]
@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>
Title: Re: time
Post by: Sidewinder on March 31, 2010, 06:02:56 PM
Not to be too obvious, but I think you need a plan B:

Quote
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)
Title: Re: time
Post by: Helpmeh on March 31, 2010, 06:16:18 PM
Not to be too obvious, but I think you need a plan B:

What happens if the minutes value is 59?

Just asking.  8)
Quote
C:\batch> addminute.bat
start time=19:59:35.10
Hour=18
minutes=59
minutes plus 1=60
C:\batch>
Title: Re: time
Post by: ghostdog74 on March 31, 2010, 06:23:23 PM
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.
Title: Re: time
Post by: Geek-9pm on March 31, 2010, 06:24:13 PM
Quote
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   ::) ::)
Title: Re: time
Post by: ghostdog74 on March 31, 2010, 06:31:03 PM
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
Code: [Select]
cscript //nologo mydateadd.vbs
Title: Re: time
Post by: greg on March 31, 2010, 07:09:58 PM
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

Code: [Select]
@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>
Title: Re: time
Post by: Helpmeh on March 31, 2010, 07:13:31 PM
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?
Title: Re: time
Post by: ghostdog74 on March 31, 2010, 07:35:08 PM
Code: [Select]
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.
Title: Re: time
Post by: Geek-9pm on March 31, 2010, 07:47:04 PM
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
Title: Re: time
Post by: ghostdog74 on March 31, 2010, 07:52:22 PM
hah, yes you are right. its 23:59
Title: Re: time
Post by: greg on March 31, 2010, 08:28:03 PM
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

Code: [Select]
@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%
Title: Re: time
Post by: ghostdog74 on March 31, 2010, 08:33:43 PM
using your new code and setting TIME variable to 23:58.59.00 gives me
Code: [Select]
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.
Title: Re: time
Post by: greg on March 31, 2010, 08:40:36 PM
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."
Title: Re: time
Post by: ghostdog74 on March 31, 2010, 08:54:43 PM
The batch solution is the best solution.
yeah right. in a million years

Quote
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.
Title: Re: time
Post by: greg on March 31, 2010, 09:02:14 PM
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.

Title: Re: time
Post by: Helpmeh on March 31, 2010, 09:10:54 PM
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.
Title: Re: time
Post by: Geek-9pm on March 31, 2010, 09:21:37 PM
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
Title: Re: time
Post by: BC_Programmer on April 01, 2010, 04:34:12 AM

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
Code: [Select]
@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.

Quote
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.


Title: Re: time
Post by: Sidewinder on April 01, 2010, 07:19:22 AM
Quote
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
Code: [Select]
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)
Title: Re: time
Post by: BC_Programmer on April 01, 2010, 07:43:03 AM
Quote
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.
Title: Re: time
Post by: ghostdog74 on April 01, 2010, 07:46:51 AM
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.
Title: Re: time
Post by: greg on April 01, 2010, 11:19:34 AM
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]
Title: Re: time
Post by: Sidewinder on April 01, 2010, 03:19:07 PM
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.

Quote
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)
Title: Re: time
Post by: Helpmeh on April 01, 2010, 04:29:07 PM
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...
Title: Re: time
Post by: BC_Programmer on April 01, 2010, 04:47:07 PM
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.
Title: Re: time
Post by: greg on April 01, 2010, 04:58:29 PM

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.

Title: Re: time
Post by: mat123 on April 01, 2010, 06:02:31 PM
greg your batch solution worked
Title: Re: time
Post by: Salmon Trout on April 02, 2010, 04:03:06 AM
your thunder

Damp firecracker more like.
Title: Re: time
Post by: greg on April 02, 2010, 01:14:27 PM
Damper.


Matt wrote:

"Greg, your batch solution worked."
Title: Re: time
Post by: BC_Programmer on April 02, 2010, 01:37:44 PM
Matt wrote:

"Greg, your batch solution worked."

Very good!

Do you remember how many sides a Triangle has too?
Title: Re: time
Post by: greg on April 02, 2010, 02:04:56 PM
Very good!

Do you remember how many sides a Triangle has too?

A Triangle has four sides and 360 degrees.
Title: Re: time
Post by: BC_Programmer on April 02, 2010, 02:08:39 PM
A Triangle has four side and 360 degrees.

I certainly hope that is a attempt at humour.
Title: Re: time
Post by: greg on April 02, 2010, 02:15:11 PM
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)


Title: Re: time
Post by: Salmon Trout on April 02, 2010, 02:27:37 PM
A Triangle has four sides and 360 degrees.

Those are facts.


Greg, the Pythagoras de nos jours.

Title: Re: time
Post by: greg on April 02, 2010, 06:59:09 PM
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
Code: [Select]
@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>
Title: Re: time
Post by: Carbon Dudeoxide on April 03, 2010, 05:17:42 AM
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?
Title: Re: time
Post by: greg on April 03, 2010, 06:21:14 AM
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.
Title: Re: time
Post by: Salmon Trout on April 03, 2010, 06:24:34 AM
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.
Title: Re: time
Post by: greg on April 03, 2010, 06:35:31 AM

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?"
Title: Re: time
Post by: Salmon Trout on April 03, 2010, 06:37:35 AM
Where are Geg's meds? It sounds like it's that time of day.
Title: Re: time
Post by: Carbon Dudeoxide on April 03, 2010, 07:25:11 AM
There we go.