0

I am new to VBScript and can't figure out why I'm getting an Object Required error with my code. It's very simple right now, I've just begun it:

<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
Dim dateandtime
On Error Resume Next
set dateandtime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
If Err <> 0 Then
  ' File operation(s) failed, handle the error
  response.write(Err.Description)
End If
%>

Why am I not able to set the DateTime? I've set the FileSystemObject for use later in the code FYI. I'm just putting it all in here so you see exactly what I have. I figure it's a simple syntax thing but I can't seem to find the answer anywhere. Thanks for your help!

MillerMedia
  • 3,651
  • 17
  • 71
  • 150
  • possible duplicate of ["Object required" when using Set in an assignment](http://stackoverflow.com/questions/23633643/object-required-when-using-set-in-an-assignment) – user692942 May 14 '14 at 08:35
  • 1
    Same issue use of `Set` on data types that are not objects (in VBScript). Also use of .Net syntax `DateTime.Now.ToString()` should be `Now()` for Classic ASP equivalent or a custom date format. See [Answer to Classic ASP - Format current date and time](http://stackoverflow.com/a/22575530/692942). – user692942 May 14 '14 at 08:38

2 Answers2

2

The first mistake is to use Set when assigning a non-object to a variable. The last entry in the 'Related' list “Object required” when using Set in an assignment deals with it.

>> Set dt = "a string"
>>
Error Number:       424
Error Description:  Object required [because Set wants an object to assign]

No Set, no problem:

>> dt = "a string"
>> WScript.Echo dt
>>
a string

Removing the Set will reveal the next problem: Unless you defined a suitable class and an instance named 'DateTime' of your own, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") will fail with the same error 424.

>> dt = Nix.Now.ToString("")
>>
Error Number:       424
Error Description:  Object required [to use its .Now member]

Do a search here for ways to format a date in VBScript. (First hit for "[vbscript] format date": date format in VBS)

The () in the .Write call should be removed; they are not parameter list (), but 'pass me per value' (). See this answer and follow the link to Eric Lippert's article.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • The OP is mixing .Net syntax and Classic ASP syntax hence the `DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")`. – user692942 May 14 '14 at 08:34
  • 1
    I maybe bias but personally this is a better example of [Custom date formatting in Classic ASP](http://stackoverflow.com/a/22575530/692942). – user692942 May 14 '14 at 08:40
  • @Lankymart, this link is exactly what I'm looking for, thank you. Ekkehard's answer definitely shed light on a lot for me though as well. Thanks! – MillerMedia May 14 '14 at 17:23
0

What about crear your code and just do this?

<%
    dateandtime = now()

    response.write dateandtime
%>

To use setyou need a Object.

MarceloBarbosa
  • 915
  • 16
  • 29