VBS Object Required Error manipulating strings - windows

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.

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)

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

Related

How can I reformat Date Time output into ISO 8601 [duplicate]

This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 3 years ago.
Using the code below it outputs the following value: 04/10/2019 which is due to the server being set to UK date time settings. However google is reading this date as 10 April 19. So I need to convert it to ISO8601 formate, ie 10/04/2019
Dim FutureDate, TodaysDate
FutureDate = FormatDateTime(Now(),1)
TodaysDate = DateAdd("m", 0, FutureDate)
FutureDate = DateAdd("d", +100, TodaysDate)
"priceValidUntil": "<%= FutureDate %>",
I don't remember ASP supporting this, you can change the format between long date, short date, ... but not to ISO8601
I used something like this to do it (string split and concat):
d = split(FutureDate,"/")
future_iso = d(1) & "/" & d(0) & "/" & d(2)

VBScript to format the file and date <filename><date>.csv [duplicate]

This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 4 years ago.
Hi I need help to set the file name as .csv
currently we could set the file name as 2_10_2018user.csv with the below codes
Set CPY = CreateObject("Scripting.FileSystemObject")
CPY.CopyFile "\\users\user.csv", "\\home\group\" & Replace(FormatDateTime(Date(),2),"","-") & "users.csv",True
Set CPY = nothing
'MsgBox("Done")
You cannot use the ANGLE brackets in a file/folder name it is not permitted.
In fact, The following characters are NOT permitted:
/ \ < > ? * | : "
The bit which you state concerns you:
Replace(FormatDateTime(Date(),2),"","-") & "users.csv",True"
Can be broken down into the following individual commands:
FormatDateTime(Date(),2)
This is telling the system to format the system date (date today) as a SHORT DATE
The options for the second parameter are:
0 = vbGeneralDate (date and time)
1 = vbLongDate (long date)
2 = vbShortDate (short date)
3 = vbLongTime (long time)
4 = vbShortTime (short time)
So now we have the date format,
Next is the replace action :
Replace(FormatDateTime(Date(),2),"","-") & "users.csv",True"
where we replace the date delimiters in this case "" (null string) with a hyphen "-" (-)
So this part of the filename changes from 010119 to 01-01-19 (for example 1st Jan 2019)
and "users.csv" is attached to the end of it all
Finally the TRUE comment tells the system to OVERWRITE the file if it already exists!
If you want the filename before the date then simply swap the bits over:
CPY.CopyFile "\\users\user.csv", "\\home\shared\group\" & "users-" & Replace(FormatDateTime(Date(),2),"","-") & ".csv",True

DateSerial overflow with dates greater than 17/09/2059

I am currently working on a VB6 project that handles event data transmit by UK rail stock. Occasionally the trains 'gets confused' about the date and will transmit events dated wildly in the future, I have seen events dated as far as 2088. The date is transmit as Unix time (seconds from 1/1/1970).
I understand what the issue is, i am just struggling to find a solution. The issue appears to be when the date exceeds '17/09/2059' it overflows the integer used for the 'day' that DateSerial can handle. The code below is the line where the overflow occurs, so when 'intDays+1' is > 32767.
UnixTimestampToDateTime = DateSerial(1970, 1, intDays + 1) + TimeSerial(intHours, intMins, CInt(intSecs))
The goal is to convert Unix time into the following format "dd/mm/yyyy hh:mm:ss". Can i get DateSerial to work beyond this date limitation or do i need to completely change how i calculate the date? Any help would be appreciated. Cheers.
You could initialize your result to 01/01/1970 and then add the required seconds:
Dim unix_time As Currency
Dim max_long As Long
Dim result As Variant
' Determine unix time
unix_time = .....
' Initialize result to 01/01/1970 00:00:00
result = DateSerial(1970, 1, 1) + TimeSerial(0, 0, 0)
' Determine maximum number of seconds we can add in a single call
max_long = 2147483647
' Add desired time
While unix_time > max_long
result = DateAdd("s", max_long, result)
unix_time = unix_time - max_long
Wend
result = DateAdd("s", CLng(unix_time), result)
Actually it is quite trivial to come up with a replacement version of DateSerial that accepts Long for days, e.g. try this:
Private Function MyDateSerial(ByVal Year As Integer, ByVal Month As Integer, ByVal Day As Long)
MyDateSerial = DateSerial(Year, Month, 1) + Day - 1
End Function
Here is a simple use-case to test it
Debug.Print MyDateSerial(1970, 1, 30000), DateSerial(1970, 1, 30000)
19.2.2052 19.2.2052

How to find the week number of a given date in Visual Basic 6?

Has anyone a working function that returns the week number of a given date in vb6?
This used to work:
Dim W As Integer
W = Format(DateSerial(2010, 1, 1), "ww", vbMonday, vbFirstFourDays)
But in Windows 8.1 you now get "Out of stack space".
The answer is a bit more complex given the offsets
The following function should work well for what you need. Simply pass it a valid Date object, and it will return an Integer of the Week number.
Private Function Week(dteValue As Date) As Integer
'Monday is set as first day of week
Dim lngDate As Long
Dim intWeek As Integer
'If january 1. is later then thursday, january 1. is not in week 1
If Not Weekday("01/01/" & Year(dteValue), vbMonday) > 4 Then
intWeek = 1
Else
intWeek = 0
End If
'Sets long-value for january 1.
lngDate = CLng(CDate("01/01/" & Year(dteValue)))
'Finds the first monday of year
lngDate = lngDate + (8 - Weekday("01/01/" & Year(dteValue), vbMonday))
'Increases week by week until set date is passed
While Not lngDate > CLng(CDate(dteValue))
intWeek = intWeek + 1
lngDate = lngDate + 7
Wend
'If the date set is not in week 1, this finds latest week previous year
If intWeek = 0 Then
intWeek = Week("31/12/" & Year(dteValue) - 1)
End If
Week = intWeek
End Function
This code is courtesy of FreeVBCode.com (included a reference to give attribute to the original author).

Getting month and year date format in vbscript

Can someone please tell me how can we get data in the month and date format.In the below code it shows me the year with the same date one year ago in the format 6/18/2012 ..but I just need the month/year.
LastMonth = DateAdd("m",-12,Date)
Thanks
Use the specialized functions Month(), Year(), ... to get at the private parts of a (variable of type) Date:
>> dt = DateAdd("m",-12,Date)
>> WScript.Echo TypeName(dt), CStr(dt), Month(dt) & "/" & Year(dt)
>>
Date 6/18/2012 6/2012

Resources