0

I am trying to create a simple script that prompts the user for their birthday, formatted as 10.02.20, then takes that string and turns it into text, such as October 2nd 2020. I have the following code:

Dim bd, message, title ' define variables
title = "What is your birthday?" ' set variable
message = "Format like so: 12.15.07; 3.03.05" ' set variable

bd = InputBox(message, title) ' prompt user

Dim year
year = bd.Substring(6, 2)
Dim month
month = bd.Substring(0, 2)
Dim day
day = bd.Substring(3, 2)

msgbox bd ' for testing
call msgbox(year + month + day) 'also testing

And I am getting an error after the prompt, ...\Desktop\test.vbs(8, 1) Microsoft VBScript runtime error: Object required: '12.23.03' and I am not sure what it means, Object Required.

Any fixes or suggestions would be very much appreciated.

Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29
  • 1
    Does this answer your question? [Format current date and time in VBScript](https://stackoverflow.com/questions/22574092/format-current-date-and-time-in-vbscript) – user692942 Oct 03 '20 at 18:50
  • `SubString()` is not supported you should use `Mid()`. – user692942 Oct 03 '20 at 18:51
  • The error is because you try to call a method `SubString()` on a variable that isn’t an object reference as it wasn’t instantiated with `Set bd = ...` hence `Object required`. – user692942 Oct 04 '20 at 07:51

2 Answers2

0

You cannot use Substring in VBScript. You can use Mid instead:

Dim year
year = Mid(bd, 7, 2)

The first character in a string is at position 1, not 0, so I adjusted your parameter from 6 to 7.

Also, to concatenate strings, although + works, you can also use &:

call msgbox(year & month & day)
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29
0

You can use this function in vbscript : FormatDateTime(date,format)

Parameter Description date Required.

Any valid date expression (like Date() or Now()) format Optional.

A value that specifies the date/time format to use can take the following values:

0 = vbGeneralDate - Default. Returns date: mm/dd/yyyy and time if specified: hh:mm:ss PM/AM.
1 = vbLongDate - Returns date: weekday, monthname, year
2 = vbShortDate - Returns date: mm/dd/yyyy
3 = vbLongTime - Returns time: hh:mm:ss PM/AM
4 = vbShortTime - Return time: hh:mm

Dim Title,Input
Title = "format date"
Input = InputBox("Enter your date of Birthday !",Title,"10.02.20")
Input = FormatDateTime(Replace(Input,".","/"),1)
MsgBox Input
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Using `FormatDateTime()` gives you very little control over to output format, this has been covered so many times but instead of flagging as a duplicate you still go ahead and answer. They aren’t even using correct syntax in the code example they posted. – user692942 Oct 03 '20 at 18:53