×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

DateAdd Error in script (one of many)

DateAdd Error in script (one of many)

DateAdd Error in script (one of many)

(OP)
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 --> vbscript

'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

RE: DateAdd Error in script (one of many)

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!

https://msdn.microsoft.com/en-us/library/windows/d...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close