Date archives - The VBScript Network and Systems Administrator's Cafe

The VBScript Network and Systems Administrator's Cafe:

date

Feb 10 2009   6:11PM GMT

Easily compare dates from with in VBScript



Posted by: Jerry Lees
VBScipt, DATEDIFF, date, Dates, Date Comparison, VBScript Functions

On occasion you need to compare two dates in your scripts, this can be a real challenge when you approach the date as if it were a string and/or compare the date parts to one another. Then finding the difference can be a whole other set of challenges if your dates span several months, days, or years.

Luckily, there is a function right into VBScript that helps you compare two dates to one another and returns the difference in a unit of time that you specify! That function is the datediff function!

Below is an example that returns the number of seconds, minutes, hours, days, and years between now and February 25, 1973.

Option Explicit

Dim DateThen, DateNow

DateThen = “2/25/1973″
DateNow = Now

WScript.Echo DateDiff(”s”,DateThen, DateNow)
WScript.Echo DateDiff(”n”,DateThen, DateNow)
WScript.Echo DateDiff(”h”,DateThen, DateNow)
WScript.Echo DateDiff(”d”,DateThen, DateNow)
WScript.Echo DateDiff(”yyyy”,DateThen, DateNow)

Enjoy!

Nov 10 2008   4:01AM GMT

Getting the date and time from a remote system via WMI and the WIN32_LocalTime class



Posted by: Jerry Lees
VBScript, date, systems management, win32_localtime

Recently, after the time change I had to log into around 30 to 40 servers to check that the date was correct. This was a real pain, since it required me to actually log onto the server via terminal services and run a command prompt to issue a date command.

Unfortunately, I didn’t have a script written to do this remotely with WMI and I needed to get it done very quickly… so I had to do it the long, old fashioned, and unlazy way. Shortly after this I wrote the following function to check the date on a remote server using the WMI Win32_LocalTime class! Notice the class name is “Local Time”, and that’s just what it is… the local time of the system. However, if you run it on a remote system it returns the local time of the remote system!

Here is the function I wrote:

WScript.Echo GetDate(”.”)

Function GetDate(strcomputer)
on Error resume next
Set objWMIService = GetObject(”winmgmts:{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)

Set ColDate = objWMIService.ExecQuery(”Select * from Win32_LocalTime”)

for each objDate in ColDate
GetDate = ObjDate.Month & “/” & ObjDate.Day & “/” & ObjDate.Year
Next

‘cleanup memory
objWMIService = Nothing
End Function