Solved

How do I calculate time 4 hours ago in vbs or batch

Posted on 2006-10-26
7
564 Views
Last Modified: 2012-06-21
I need to calculate the time 4 hours ago in a batch file or vbs and set it to a system variable.

Time now is 0900
fourHoursAgo = 0500

time now is 0200
fourHoursAgo = 2200
0
Comment
Question by:brian_appliedcpu
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Expert Comment

by:nicolasn
ID: 17812313
Hello Brian,

The code below will popup a message with the time four hours ago in the format you are looking for.  Tell us a little more about the "system variable" part though... Do you mean an enviroment variable?

Nicolas Noakes

***** CODE BELOW ****

Function Pad(s,l,c)
's = String to pad (with prefix)
'l = Target length
'c = Character to pad with
Dim p
Dim i
  p = s
  Do While Len(p) < l
    p = c & p
  Loop
  Pad = p
End Function

'You can use the following units:
'y = year
'm = month
'd = day
'h = hour
'n = minute
's = second

TgtTime = DateAdd("h", -4, Now())

MsgBox Pad(Hour(TgtTime), 2, "0") & Pad(Minute(TgtTime), 2, "0")
0
 
LVL 2

Author Comment

by:brian_appliedcpu
ID: 17812538
Yes, I apologize...

We have a script that calculates the hour as shown below but I need something that can subtract 4 hours from it and set it as an environment variable.

' from the  RunNow.vbs file in the question above, modified to display instead of setting:
Wscript.echo "set YY=" & Right("0" & Year(now()), 2)
Wscript.echo "set MM=" & Right("0" & Month(now()), 2)
Wscript.echo "set DD=" & Right("0" & Day(now()), 2)
Wscript.echo "set Hour=" & Right("0" & Hour(now()), 2)
Wscript.echo "set Min=" & Right("0" & Minute(now()), 2)
Wscript.echo "set Sec=" & Right("0" & Second(now()), 2)
Wscript.echo "set Now=%Hour%-%Min%-Hours"
'  continue the rest


Thanks
0
 
LVL 1

Expert Comment

by:nicolasn
ID: 17812959
To subtract 4 hours, set a variable (TgtTime in my first post) to the current time less 4 hours by using the vbscript DateAdd function as follows:

TgtTime = DateAdd("h", -4, now())   'This will set TgtTime to the current time less 4 hours

Now replace every instance of now() in your script with TgtTime to make it use the 4-hours-ago time.

I'll have to get back to you re the environment variable part...

Nicolas
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:brian_appliedcpu
ID: 17813601
Not quite sure what I am doing wrong...

Wscript.Echo "set h= " & DatePart("h", now)
Wscript.echo "set TgtTime=" & DateAdd("h", -4, now())

Wscript.echo "set HSub4=" & Right("0" & Hour(TgtTime), 2)
Wscript.echo "set MSub4=" & Right("0" & Minute(TgtTime), 2)
Wscript.echo "set SSub4=" & Right("0" & Second(TgtTime), 2)

Wscript.Echo "set TgtHour= " & DatePart("hour", TgtTime)
Wscript.echo "set TgtHour=%TgtHour%"


Here is the output  ALL 00   ?????

F:\bin>set h= 13

F:\bin>set TgtTime=10/26/2006 9:20:33 AM

F:\bin>set HSub4=00

F:\bin>set MSub4=00

F:\bin>set SSub4=00
0
 
LVL 4

Expert Comment

by:mattcarver
ID: 17814458
TimeNow=now()
TgtTime=DateAdd("h",-4, TimeNow)

Wscript.Echo "set h= " & DatePart("h", TimeNow)
Wscript.echo "set TgtTime=" & TgtTime
Wscript.echo "set HSub4=" & Right("0" & DatePart("h",TgtTime), 2)
Wscript.echo "set MSub4=" & Right("0" & DatePart("m",TgtTime), 2)
Wscript.echo "set SSub4=" & Right("0" & DatePart("s",TgtTime), 2)

Wscript.Echo "set TgtHour= " & DatePart("h", TgtTime)
0
 
LVL 4

Accepted Solution

by:
mattcarver earned 250 total points
ID: 17814487
I posted the working version above.  What you were doing wrong was trying to use TgtTime but TgtTime was never set "in VBS/WSH".  

The following line would set a variable in the context of a batch file but not for VBS/WSH.
Wscript.echo "set TgtTime=" & DateAdd("h", -4, now())
0
 
LVL 2

Author Comment

by:brian_appliedcpu
ID: 17814494
Excellent...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now