Home / Microsoft / Microsoft DOS / time
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2 3 4  All - (Bottom) Print
Author Topic: time  (Read 3963 times)
mat123
Topic Starter
Hopeful



Thanked: 16
Posts: 228

Experience: Familiar
OS: Windows XP

1 1 1
« on: March 31, 2010, 12:12:10 AM »

how can i add one minute to the current time and store it as a variable
IP logged



T.C.
Beginner



Thanked: 11
Posts: 109




« Reply #1 on: March 31, 2010, 01:12:47 AM »

Start by advising the format of your system time as returned when time/t is entered.
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #2 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'

IP logged

BC_Programmer
Mastermind


Thanked: 697
Posts: 15,865

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #3 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)
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #4 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. :)
IP logged

BC_Programmer
Mastermind


Thanked: 697
Posts: 15,865

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #5 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)



IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #6 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.
IP logged

mat123
Topic Starter
Hopeful



Thanked: 16
Posts: 228

Experience: Familiar
OS: Windows XP

1 1 1
« Reply #7 on: March 31, 2010, 08:57:57 AM »

can any of the codes store the new time in dos.
IP logged



greg
Intermediate



Thanked: 7
Posts: 183


« Reply #8 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>
IP logged

Have a Nice Day
Sidewinder
Guru



Thanked: 97
Posts: 4,340

Experience: Familiar
OS: Windows 7

« Reply #9 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)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Helpmeh
Egghead



Thanked: 117
Posts: 3,607

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #10 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>
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #11 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.
IP logged

Geek-9pm
Sage



Thanked: 373
Posts: 8,916

Computer: Specs
Experience: Expert
OS: Windows XP


Geek After Dark

Geek 9pm blog
« Reply #12 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
   ::) ::)
IP logged

ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #13 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
IP logged

greg
Intermediate



Thanked: 7
Posts: 183


« Reply #14 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>
IP logged

Have a Nice Day
Pages: [1] 2 3 4  All - (Top) Print 
Home / Microsoft / Microsoft DOS / time « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 3.168 seconds with 20 queries.