Current Time + 1 Minute with VBScript?

Joined
Oct 23, 2008
Messages
48
Hi,

I have figured out how to display the time with vbscript and how to add two numbers together, but how do i add 1 minute to the current time?

eg. The time is 12:40pm... i want the script to display 12:41, can this be done?

Thanks!

Regards, DrunkenSensei91
 
I mean i'd like to view the current time but add on 1 minute. That dateadd thing seems to just add on time from whatever date you set... i'd like to use the current time. :)
 
Then just get the current time with Date(). Take the result of that function and pass it as the argument to DateAdd(). This is programming--composing new functions from available features.
 
dim mid

mid1 = msgbox (""&minute(time)&"",6,"The Minute")
mid2 = msgbox (DateAdd("n",1,""&mid1&""))

The first message box states the current minute of the hour... and the second one says 12/31/1899 12:01:00 AM... Am i writing this script correctly?
 
I don't know. Syntactically, it's correct. Semantically, I can't tell -- what is it that you want the script to do?

In your first post, you said you cared only about the current time. Do you also care about the current date and time? Then use "Now()" in place of 'time":

Code:
msgbox (DateAdd("n",1,Now()))
 
Vage i think yours does sort of what i want it to do but it displays the hour also... and it doesn't actually show in a message box. I tried to get it to show in a message box but got syntax errors happening. How can i do this?
 
I'm making a vbscript which creates a batch file that adds a scheduled task on my computer 1 minute after i run the application... i don't know how to get it to do current minute + 1
 
Vage i think yours does sort of what i want it to do but it displays the hour also... and it doesn't actually show in a message box. I tried to get it to show in a message box but got syntax errors happening. How can i do this?

Well you can simply take the hour part out of mine. Is this something like what you want?

Code:
myDate = Minute(Now()-1)
WScript.Echo myDate

That should display just the current minute minus 1 in a message box for you.
 
That should display just the current minute minus 1 in a message box for you.
It won't. You're subtracting one from the time value, which means you're losing one second into the past--not adding one minute into the future.

You haven't been very forthcoming, Drunken, but I assume you've rejected the previous solutions you've been offered because they don't format the time correctly--providing the hour and minute only--and you don't know how to fix them. If you want only the hour and minute for one minute from now, you can do this:

Code:
when = DateAdd("n",1,Now())
WScript.Echo FormatDateTime(when, 4)
You might want the seconds to be included. After all, this code might run the program a couple seconds from now instead of one minute from now, if you execute it late in the minute. If so, then you'll need something more precise:
Code:
when = DateAdd("n",1,Now())
spc = InStr(when, " ")
WScript.Echo MID(when, spc+1)
WScript.Echo will write to the console, and you'e previously indicated you want to make a message box. If you really do want a mesage box, use MsgBox, instead.
 
It won't. You're subtracting one from the time value, which means you're losing one second into the past--not adding one minute into the future.

Right right, I thought he wanted to go back 1 minute, change mine to plus 1 then.
 
The code is still wrong.

How is it wrong, that code will display the minute plus 1, I'm looking at it work right now:confused:

Edit: Duh, I just went back and looked, mistakenly put the modifier inside of minute(), oops. put it outside and it works.
 
You're subtracting (or adding) one second, not one minute. Here is your code compared to the corrected code, along with code that prints the full current time for reference:

Code:
WScript.Echo Now()		' show the current time
myDate = Minute(Now()+1)	' your fixed code, still wrong
WScript.Echo myDate
myDate = Minute(Now())+1	' your code corrected, adding one minute
WScript.Echo myDate

When I run it, it generates this output:

Code:
C:\Users\mikeblas>cscript foo2.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

11/5/2008 11:37:10
37
38

As you can see, you're adding one second, then truncating back to the minute value. Worse, if it's 59 seconds, you'll print 60, which isn't exactly a valid value.
 
Since I have to interject PowerShell anytime I hear "How do I ____ in VBScript..."

$time = (get-date).addminutes(1)
 
Back
Top