VBS Msgbox(DateTime.Now.ToString("yyyyMMdd HH:mm:ss")) show error - vbscript

Please help. My code just simple but show error as in the picture.
Msgbox DateTime.Now.ToString("yyyyMMdd HH:mm:ss")

There is no DateTime object in VBScript, just plain Date variables and functions:
>> d = Date()
>> n = Now()
>> WScript.Echo TypeName(d), d, TypeName(n), n
>> WScript.Echo TypeName(Month(n)), Month(n)
>> s = FormatDateTime(n)
>> WScript.Echo TypeName(s), s
>>
Date 21.06.2017 Date 21.06.2017 05:58:13
Integer 6
String 21.06.2017 05:58:13
You can use a .NET System.Text.StringBuilder to do more fancy formatting. (cf here)

Related

how to convert a number into mm:ss format using VBScript

I want to convert a number (less than 3600) in mm:ss format using VBScript. The main issue that i am facing is to add leading zeroes in case i get a single digit.
For example:-
while trying to convert 306 in mm:ss format i get the output as 5:6 instead of 05:06.
This is my code..
entered_time = "306"
quotient = entered_time/60
quotient = Int(quotient)
MsgBox quotient
remainder = entered_time Mod 60
MsgBox remainder
time_format = quotient&":"&remainder
msgbox time_format
Thanks for the help in advance.
Try like this ;)
intTotalSecs = 306
MsgBox intTotalSecs & "(s) ===> " & ConvertTime(intTotalSecs),vbinformation,"output time format as hh:mm:ss"
'************************************************************
Function ConvertTime(intTotalSecs)
Dim intHours,intMinutes,intSeconds,Time
intHours = intTotalSecs \ 3600
intMinutes = (intTotalSecs Mod 3600) \ 60
intSeconds = intTotalSecs Mod 60
ConvertTime = LPad(intHours) & " h : " & LPad(intMinutes) & " m : " & LPad(intSeconds) & " s"
End Function
'************************************************************
Function LPad(v)
LPad = Right("0" & v, 2)
End Function
'************************************************************
What about a simple if remainder < 10 ?
If your locale/regional settings are suitable, you just do:
>> setlocale "de-de"
>> secs = 308
>> ts = TimeSerial(0, 0, secs)
>> WScript.Echo FormatDateTime(ts, vbLongTime)
>>
00:05:08
If that's not possible, put some effort in a .NET based format class like this one.

How to remove the seconds from timeValue function

I'm looking to print out the time 6:00am without the seconds. Using TimeValue it includes the seconds. Is there away to do this without including the seconds?
CurrentTime = TimeValue("6:00 am")
Use built-in FormatDateTime with vbShortTime :
CurrentTime = FormatDateTime("6:00:31 am", vbShortTime)
Use a RegExp to zap the offending seconds from the string representation of the date:
>> Set r = New RegExp
>> r.Pattern = ":\d\d "
>> dtCurrentTime = TimeValue("6:00 PM")
>> sCurrentTime = CStr(dtCurrentTime)
>> sCurrentTime = r.Replace(sCurrentTime, " ")
>> WScript.Echo qq(sCurrentTime)
>>
"6:00 PM"
dtCurrentTime = TimeValue("6:00 PM")
sCurrentTime = CStr(dtCurrentTime)
sCurrentTime=Replace (sCurrentTime,":00:00 PM",":00 PM")
MsgBox sCurrentTime
I had the same problem. But I use Mid, Left & Right functions to solve it.
ThisTime = TimeValue(YourTime)
TimeSpace = InStr(1,ThisTime," ")
ThisShortTime = Left(ThisTime,TimeSpace-4) & Right(ThisTime,3)
InStr looks for the space within the time.
Left picks up the left side of the time, minus the second.
Right picks up the last 3 characters of the time, eg " AM" or " PM".

Date issue in vb script [duplicate]

This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 4 years ago.
My issue is when I retrieve date back it gives me in this format:
lastseenstatus=rsprefobj("lastseentstamp")
19-07-2014 15:31:32
I want it in 7/19/2014 3:31:32 PM format with AM/PM intact.
Please help..
First and foremost you need to determine the data type of rsprefobj("lastseentstamp"):
MsgBox TypeName(rsprefobj("lastseentstamp"))
If it's a string, you need to convert it to a datetime value first:
lastseenstatus = CDate(rsprefobj("lastseentstamp"))
If you want the date formatted according to the system's regional settings, use the FormatDateTime() function as #John suggested:
MsgBox FormatDateTime(lastseenstatus)
If you need a distinct date format regardless of the system's regional settings you have to either build the formatted string yourself:
Function LPad(v) : LPad = Right("00" & v, 2) : End Function
Function FormatDate(d)
formattedDate = Month(d) & "/" & LPad(Day(d)) & "/" & Year(d) & " " & _
((Hour(d) + 23) Mod 12 + 1) & ":" & LPad(Minute(d)) & ":" & _
LPad(Second(d))
If Hour(d) < 12 Then
formattedDate = formattedDate & " AM"
Else
formattedDate = formattedDate & " PM"
End If
FormatDate = formattedDate
End Function
MsgBox FormatDate(lastseenstatus)
or use the .Net StringBuilder class:
Set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat "{0:M\/dd\/yyyy h:mm:ss tt}", lastseenstatus
MsgBox sb.ToString()
In my tests I wasn't able to get the tt format specifier to work, though, so you may have to resort to something like this:
Set sb = CreateObject("System.Text.StringBuilder")
If Hour(lastseenstatus) < 12 Then
am_pm = "AM"
Else
am_pm = "PM"
End If
sb.AppendFormat_5 Nothing, "{0:M\/dd\/yyyy h:mm:ss} {1}", _
Array(lastseenstatus, am_pm)
MsgBox sb.ToString()
I'm assuming you are using VBScript and not VB.NET like you have tagged.
Use FormatDateTime(lastseenstatus).
That should give you the format "2/16/2010 1:45:00 PM".

How to search between dates?

iam using ms access database and i want to get data between two dates
stdate= starting date
edate=end date
iam using the code below.
Private Sub Label3_Click()
DataGrid1.Visible = True
Dim stdate As Date
Dim edate As Date
Format = stdate("mm/dd/yyyy")
Format = edate("mm/dd/yyyy")
stdate = Text1.Text
edate = Text2.Text
Adodc1.RecordSource = "SELECT * FROM sales_info WHERE date BETWEEN &stdate& and &edate& "
Adodc1.Refresh
End Sub
errors iam getting are:
syntax error expected array(in format statement)
missing operator in query expression date.
thanks
plz help
iam a beginner
This code should work-
Private Sub Label3_Click()
DataGrid1.Visible = True
Dim stdate As String
Dim edate As String
stdate = Format(Text1.Text, "mm/dd/yyyy")
edate = Format(Text2.Text, "mm/dd/yyyy")
Adodc1.RecordSource = "SELECT * FROM [sales_info] WHERE [date] BETWEEN #" & stdate & " and " & edate & "]"
Adodc1.Refresh
End Sub
I also agree with #Joe in that you should not use date for your column name as it is typically a reserved word. The [] get around that, but it will make life tougher in the long run.
try to change name of your field because date is a reserved keyword.
After change the format of your date to use a format compliant to VB6 (with #)
The lines Format = stdate("mm/dd/yyyy") and Format = edate("mm/dd/yyyy")
don't look like valid VB6 to me.
You will need to build the query string by gluing the bits together, the interpreter cannot see inside the quotation marks to understand that you want a variable there
"SELECT * FROM sales_info WHERE date BETWEEN #" & stdate & "# and #" & edate &"#"
Try if this works. :)
Adodc1.RecordSource = = "SELECT * " & "FROM tbl " & _
"WHERE Date >= #" & stdate & "# AND Date <= #" & edate & "# "
Adodc1.Requery
Table_1BindingSource.Filter="DateColumnName>=# & DateTimePicker1.text & "# and DateColumName<=#" & DateTimePicker2.text & "#"

vbscript dictonary problem

I am using dictonary in VBscript. I have some problems that I don't understand some of the behaviour:
Dim CmdData
Set CmdData = CreateObject("System.Dictonary")
CmdData.Add "11", "tttzz"
CmdData.Add "sssid", "KRN"
WScript.Echo(" ZZZZZZZZ= " & CmdData.Count) 'It prints zero and not 2
Dim s
s = CmdData.Item("11")
alert(s)
WScript.Echo(s) 'It prints empry box and not tttzz
Dim a, j
a = CmdData.Keys
For j = 0 To CmdData.Count -1
WScript.Echo(" ZZZZZZZZ= " & CmdData.Count)
WScript.Echo(a(j)) ' doesn not print
Next
If (CmdData.Exists("-ad")) Then
'WScript.Echo (" RR ") ' It prints it although not in the dictonary
End If
Thanks
VBScript's Dictionary is "Scripting.Dictionary":
>> set syd = CreateObject("System.Dictionary")
>> syd.add "a",1
>>
Error Number: 429
Error Description: ActiveX component can't create object
>> set scd = CreateObject("Scripting.Dictionary")
>> scd.add "a",1
>> WScript.Echo scd.Count, scd("a")
>>
1 1

Resources