Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateAdd Error in script (one of many)

Status
Not open for further replies.

pargit

IS-IT--Management
Mar 20, 2016
1
IL
Hi,
I have a script that uses DateAdd several times.
i have single location of the function that fail with error: Invalid procedure call or argument: 'DateAdd'.
my complete code:
Code:
'On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
Computer = "%COMPNAME%"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set Win32LT = objWMIService.ExecQuery("SELECT * FROM Win32_LocalTime", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

Dim LMonth, Day, Year, Hour, Minute, Second
For Each objItem In Win32LT
	LMonth = objItem.Month
	Day = objItem.Day
	Year = objItem.Year
	Hour = objItem.Hour
	Minute = objItem.Minute
	Second = objItem.Second
Next

Set objWMIService1 = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set TimeZone = objWMIService1.ExecQuery("SELECT * FROM Win32_TimeZone", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

Dim DaylightDay, StandardDayOfWeek, DaylightMonth, StandardMonth, StandardDay, DayTime, STNDTime, DSTHour, STNDHour, DaylightDayOfWeek, CurrentYear
CurrentYear = "2016"

For Each objItem In TimeZone
	DayTime = objItem.DaylightName
	STNDTime = objItem.StandardName
	DSTHour = objItem.DaylightHour
	STNDHour = objItem.StandardHour
	DaylightDay = objItem.DaylightDay
	StandardDayOfWeek = objItem.StandardDayOfWeek
	DaylightMonth = objItem.DaylightMonth
	StandardMonth = objItem.StandardMonth
	StandardDay = objItem.StandardDay
	DaylightDayOfWeek = objItem.DaylightDayOfWeek

    select case DaylightDay
       case 1 
		DSTDay = "First"
       case 2 
		DSTDay = "Second"
       case 3 
		DSTDay = "Third"
       case 4 
		DSTDay = "Fourth"
       case 5 
		DSTDay = "Last"
	end select
	
	select case DaylightDayOfWeek
		case 0
			DSTDoW = "Sunday"
		case 1 
			DSTDoW = "Monday"
		case 2
			DSTDoW = "Tuesday"
		case 3
			DSTDoW = "Wednesday"
		case 4
			DSTDoW = "Thursday"
		case 5
			DSTDoW = "Friday"
		case 6
			DSTDoW = "Saturday"
	end select
	
	select case StandardDayOfWeek
		case 0
			STNDWeek = "Sunday"
		case 1 
			STNDWeek = "Monday"
		case 2
			STNDWeek = "Tuesday"
		case 3
			STNDWeek = "Wednesday"
		case 4
			STNDWeek = "Thursday"
		case 5
			STNDWeek = "Friday"
		case 6
			STNDWeek = "Saturday"
	end select
	
	select case DaylightMonth
		case 1 
			DSTMonth = "January"
		case 2
			DSTMonth = "February"
		case 3
			DSTMonth = "March"
		case 4
			DSTMonth = "April"
		case 5
			DSTMonth = "May"
		case 6
			DSTMonth = "June"
		case 7
			DSTMonth = "July"
		case 8
			DSTMonth = "August"
		case 9
			DSTMonth = "September"
		case 10
			DSTMonth = "October"
		case 11
			DSTMonth = "November"
		case 12
			DSTMonth = "December"
	end select
	
	select case StandardMonth
		case 1 
			STNDMonth = "January"
		case 2
			STNDMonth = "February"
		case 3
			STNDMonth = "March"
		case 4
			STNDMonth = "April"
		case 5
			STNDMonth = "May"
		case 6
			STNDMonth = "June"
		case 7
			STNDMonth = "July"
		case 8
			STNDMonth = "August"
		case 9
			STNDMonth = "September"
		case 10
			STNDMonth = "October"
		case 11
			STNDMonth = "November"
		case 12
			STNDMonth = "December"
	end select
	
	select case StandardDay
		case 1 
			STNDDay = "First"
		case 2
			STNDDay = "Second"
		case 3
			STNDDay = "Third"
		case 4
			STNDDay = "Fourth"
		case 5
			STNDDay = "Last"
	end select
Next

Dim DDate , SDate , DFinalDate , SFinalDate 
DDate = DateSerial(CurrentYear, DaylightMonth, "01")
SDate = DateSerial(CurrentYear, StandardMonth, "01")
DDate = DateAdd("h", 2, DDate) 
SDate = DateAdd("h", 2, SDate) 

i = 0
do While i < DaylightDay
	If Weekday(DDate) = DaylightDayOfWeek then
		i = i + 1
		If i = DaylightDay then
			DFinalDate = DDate
		Else
			DDate = DateAdd("d",1,DDate)
		end if
	Else
		DDate = DateAdd("d",1,DDate)
	end if
Loop
 
If Month(DFinalDate) <> DaylightMonth then
	DFinalDate = DateAdd("d",-7,DFinalDate)
End If

i = 0
Do While i < StandardDay
	If Weekday(SDate) = StandardDayOfWeek then
		i = i + 1
		If i = StandardDay then
			SFinalDate = SDate
		Else
			SDate = DateAdd("d",1,SDate)
		end if
	Else
		SDate = DateAdd("d",1,SDate)
	end if
Loop

If Month(SFinalDate) <> StandardMonth then
	SFinalDate = DateAdd("d",-7,SFinalDate)
end if
wscript.echo Computer
wscript.echo FormatDateTime(Date)
wscript.echo FormatDateTime(DFinalDate)
wscript.echo DayTime
wscript.echo DSTDay
wscript.echo DSTDoW
wscript.echo DSTMonth
wscript.echo FormatDateTime(SFinalDate)
wscript.echo STNDTime
wscript.echo STNDDay
wscript.echo STNDWeek
wscript.echo STNDMonth
the error is at SDate = DateAdd("d",1,SDate)
any help appreciated.

thanks
mordechai
 
If DaylightDayOfWeek or StandardDayOfWeek are 0 (which they will be if they fall on a Sunday), then your Do / While loops will never end. Well, they will end... but only after you have added days to SDate / DDate to the point that they are no longer valid dates... which leads to your Invalid procedure call when trying to add one more day to 12/31/9999!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top