I have read many post but nothing really helped.
My question looks similar but bit different.
I have to change EPOC time to system time format.
data type = datetime
value= 20160630165419.634204+060
desired output
data type = datetime
value= 30/06/2016 16:54:19
as the EPOC value has digits after dot and the datatype is datetime , which makes it difficult to divide it by 10 in a loop and get ingteral value.
Please suggest solution for given input format only.
You can use this function WMIDateStringToDate for converting your date :
WScript.echo WMIDateStringToDate("20160227235343.000000+060")
WScript.echo WMIDateStringToDate("20160630165419.634204+060")
'************************************************************
Function WMIDateStringToDate(Mydate)
WMIDateStringToDate = CDate(Mid(Mydate, 5, 2) & "/" & _
Mid(Mydate, 7, 2) & "/" & Left(Mydate, 4) _
& " " & Mid (Mydate, 9, 2) & ":" & _
Mid(Mydate, 11, 2) & ":" & Mid(Mydate,13, 2))
End Function
'************************************************************
Step 1: use DateSerial() and TimeSerial() on numbers (CInt()) extracted from your input (Mid()) to get a variant of subtype Date
Step 2: use SetLocale() and Replace() to format/stringify the Date
>> SetLocale "de-de"
>> sX = "20160630165419.634204+060"
>> dtD = DateSerial(CInt(Mid(sX, 1, 4)), CInt(Mid(sX, 5, 2)), CInt(Mid(sX, 7, 2)))
>> dtT = TimeSerial(CInt(Mid(sX, 9, 2)), CInt(Mid(sX, 11, 2)), CInt(Mid(sX, 13,2)))
>> dtX = dtD + dtT
>> WScript.Echo dtX, TypeName(dtX)
>> WScript.Echo Replace(dtX, ".", "/")
>>
30.06.2016 16:54:19 Date
30/06/2016 16:54:19
Danger, Will Robinson! Look at this test script:
Function WMIDateStringToDate(Mydate)
WMIDateStringToDate = CDate(Mid(Mydate, 5, 2) & "/" & _
Mid(Mydate, 7, 2) & "/" & Left(Mydate, 4) _
& " " & Mid (Mydate, 9, 2) & ":" & _
Mid(Mydate, 11, 2) & ":" & Mid(Mydate,13, 2))
End Function
'************************************************************
For Each sLocale In Split("de-de en-us")
SetLocale sLocale
For Each sDate In Split("20160227235343.000000+060 20160203235343.000000+060")
On Error Resume Next
dtX = WMIDateStringToDate(sDate)
If Err.Number Then dtX = Err.Description
On Error GoTo 0
WScript.Echo GetLocale(), sD, sDate, dtX
Next
Next
and its output:
cscript 38249865.vbs
1031 20160227235343.000000+060 27.02.2016 23:53:43
1031 20160203235343.000000+060 02.03.2016 23:53:43
1033 20160227235343.000000+060 27.02.2016 23:53:43
1033 20160203235343.000000+060 03.02.2016 23:53:43
to see the reason for:
The sample scripts are not supported under any Microsoft standard
support program or service. The sample scripts are provided AS IS
without warranty of any kind. Microsoft further disclaims all implied
warranties including, without limitation, any implied warranties of
merchantability or of fitness for a particular purpose. The entire
risk arising out of the use or performance of the sample scripts and
documentation remains with you. In no event shall Microsoft, its
authors, or anyone else involved in the creation, production, or
delivery of the scripts be liable for any damages whatsoever
(including, without limitation, damages for loss of business profits,
business interruption, loss of business information, or other
pecuniary loss) arising out of the use of or inability to use the
sample scripts or documentation, even if Microsoft has been advised of
the possibility of such damages.
(found here)
Related
I'm getting "August 22, 2015 10:55" in a querystring and I want to convert it to "2015-08-22 10:55".
I have this so far:
datum=request.querystring("tid")
response.write datum ----this writes out August 22, 2015 10:55
datum=Cstr(datum)
DateVar = datevalue(datum)
NewDateVar = year(DateVar) & "-" & month(DateVar) & "-" & day(DateVar)
NewTimeVar=timevalue(datum)
newdatum = NewDateVar & " " & NewTimeVar
But now I get a type missmatch error on DateVar = datevalue(datum).
If I use datum="August 22, 2015 10:55" to test with, then it works.
To spell it out:
>> s = "August 22, 2015 10:55"
>> d = CDate(s)
>> WScript.Echo TypeName(d), d
>>
Date 22.08.2015 10:55:00 <-- german locale!
Added:
To get the required format, you can use the strategy described here.
DateVar = datevalue("August 22, 2015 10:55")
NewDateVar = year(DateVar) & "-" & month(DateVar) & "-" & day(DateVar) & "-" & time(DateVar)
Classic ASP tends to use US date formats, come hell or high water. However, it's easy enough to format the date you display (or pass on to the database) in whatever way you wish.
The following will pad any single-digit values with a 0. If you don't need that, then just use the plain functions, without Right().
Dim datum, mm, dd, yyyy, hh, nn, fdatum
datum = Request.Querystring("tid")
If IsDate(datum) Then
datum = CDate(datum)
mm = Right(100 + Month(datum),2)
dd = Right(100 + Day(datum),2)
yyyy = Year(datum)
hh = Right(100 + Hour(datum),2)
nn = Right(100 + Minute(datum),2)
fdatum = yyyy & "-" & mm & "-" & dd & " " & hh & ":" & nn
Else
fdatum = datum '- or whatever fallback value you want
End If
Reinstalled my dev machine with win 8.1
Enabled ASP, installed my legacy websites that I have to maintain, installed sites as an application in IIS, enabled parent paths, send errors to browser on, full detail set on in error pages. All is working well, except when I work with numbers
I'm working in a system that captures financial data inputted by users, so often a number will be: 100.0 or
100.000 or
100 or
100,00
so I have a function that (I've used on loads of websites in the past) that will standardise the irregularities into a constant format, such as 100.00 (ie: proper decimal, always using a dot, etc)
This line now fails on win 8.1 IIS 8.5: If Cdbl(myString) = Cdbl(0) Then
Function ProperDecimal(s_String)
Dim CharPlace, DotChar, iLoop, strCurrentChar, myString
myString = TRIM(s_String)
If ISNULL(myString) OR myString = "" Then myString = 0
myString = REPLACE(myString,",",".")
'Find where the comma or dot is, ie: 100.00 is in position 3 (from the right)
CharPlace = 1
DotChar = 0
For iLoop = Len(Replace(myString, " ", "")) to 1 Step -1
strCurrentChar = mid(Replace(myString, " ", ""), iLoop, 1)
If strCurrentChar = "." OR strCurrentChar = "," Then
DotChar = CharPlace
Exit For
End If
CharPlace = CharPlace + 1
Next
'If string is zero, leave it at zero, we dont need 0.00
If Cdbl(myString) = Cdbl(0) Then
'ignore it, no decimal places needed
ProperDecimal = myString
Else
'Where is the DOT
Select Case DotChar
Case 0 'eg: 100 will be converted to 100.00
ProperDecimal = (myString & ".00")
Case 1 'eg: 100. will be converted to 100.00
ProperDecimal = (myString & "00")
Case 2 'eg: 100.0 will be converted to 100.00
ProperDecimal = (myString & "0")
Case 3 'eg: 100.00 will remain
ProperDecimal = (myString)
Case Else '(4, 5, 6, 7, 8, etc) 'eg: 100.001
ProperDecimal = ProperDecimal(Round(myString,2))
End Select
End If
End Function
Call ProperDecimal("112.05")
This lead me to try other methods other than CDbl
Response.write(isNumeric(s_String)) 'works, returns false because this is a string
Response.write(CINT(myString))
Response.write(CLNG(myString))
Response.write(CDBL(myString))
Returns this:
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CINT'
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CLNG'
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CDBL'
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CINT'
If I then change the If statement to convert the 0 to a string, it works:
If myString = CSTR(0) Then
But I really dont want to be comparing strings to strings especially with numbers
This is now the second project this is happening on. The previous project I had to change the decimal comma to a fullstop (112,05 had to be 112.05) so I thought it had something to do with regional settings and the comma being listed as decimal character. But am getting different results now, its driving me up the wall.. plz assist if you can. Ta
CInt on the server works fine (2008 R2), on the clients pc (windows Xp and windows 7) but on my windows 8 machine, this is an issue.
I made my own two functions which I use now instead of CInt and CDbl. You can make more if needed. Pretty simple and tested,
EDIT:
Changed the function names toe shorter versions. Easier to replace and to use. Also does not clash with Javascripts parseInt if you do a search
Function pInt(Str)
Dim val : val = 0
'==Test Comma=='
On Error Resume Next
val = CInt(Replace(Str, ".", ","))
'==Test Period=='
On Error Resume Next
val = CInt(Replace(Str, ",", "."))
On Error Goto 0
pInt = val
End Function
Function pDbl(Str)
Dim val : val = 0
'==Test Comma=='
On Error Resume Next
val = CDbl(Replace(Str, ".", ","))
'==Test Period=='
On Error Resume Next
val = CDbl(Replace(Str, ",", "."))
On Error Goto 0
pDbl = val
End Function
I am not a pro in ClassicASP/VBScript but this works. I like my C# ASP.net
In classic ASP, I am unable to use DateAdd() with variables. I see no reason why this should work.
strTargetDate=DateAdd("d",visitDate,followDate)
visitDate is an incremental value -- 30, 60, 90, 180 etc. followDate is an actual date. However, I receive a type mismatch error using this code. Shouldn't this work??
Either visitDate isn't/can't be converted to a number, or followDate isn't/can't be converted to a date. So check the TypeName() of your input and pay attention to date formats.
Partly to show facts against #Ken's speculations:
>> s = "string"
>> WScript.Echo 0, s, TypeName(s)
>> s = DateAdd("d", 1, Now)
>> WScript.Echo 1, s, TypeName(s)
>> s = DateAdd("d", "1", CStr(now))
>> WScript.Echo 2, s, TypeName(s)
>> s = DateAdd("d", 1, "20/10/2013")
>>
0 string String
1 24.10.2013 17:05:54 Date
2 24.10.2013 17:05:54 Date
>> s = DateAdd("d", 1, "32.13.1")
>>
Error Number: 13
Error Description: Type mismatch
Update wrt comment:
As computations involving Null should propagate Null, this
>> WScript.Echo TypeName(DateAdd("d", 1, Null))
>>
Null
>>
is no surprise. While you should handle Nulls for your DateAdd() in a way appropriate to your application, they are not the cause for the type mismatch.
Empty strings (""), however, could well be the culprits:
>> WScript.Echo TypeName(DateAdd("d", 1, ""))
>>
Error Number: 13
Error Description: Type mismatch
>>
Ok I am new with writing VBScript and I want to write a string of code that plays a file (WAV format) only on a certain day and only between specific times. After piecing together multiple fragments of code I found on the internet I was left with the following:
Dim myDateString
Dim thing1
thing1 = 0
myDateString = Date()
If myDateString < "13/08/13" Then
thing1 = 1
end if
if thing1 = 1 then
If myDateString > "15/08/13" Then
thing1 = 2
end if
end if
if thing1 = 2 then
hournow = hour(Time())
If hour(Time()) >= 9 And Hour(Now()) < 22 Then
set WshShell = CreateObject("WScript.Shell")
music = "C:\Users\MYUSERNAME\Desktop\MYSOUND.wav"
WshShell.Run "wmplayer """ & music & """",0,True
Else
wscript.quit 1
End If
Else
wscript.quit 1
End If
Ok so I had set this for the date I ran this on, within the hour I was in. But
it didn't work. I expected the VBS to start playing MYSOUND.wav but it didn't. When running the file
there were no errors though, so I was wondering what I did wrong!
I running Windows 7
If anyone could tell me what I did wrong, and how to fix it that would be great.
Double points if anyone could post a corrected version of the code!
Thanks to any answers!
First, indent your code and give your variables meaningful names!
Then, your date comparison doesn't work because you're trying to compare strings as if they were dates. This usually won't work (depending on your "system locale"): you need to use date type variables and an actual date comparison function (DateDiff in VBScript).
(EDIT: as Ansgar Wiechers pointed out, you don't need to use DateDiff to compare dates in VBScript, "DateStart <= Now And Now <= DateEnd" will do just fine)
Try this:
Dim DateStart, DateEnd, WshShell, music
DateStart = DateSerial(2013, 8, 13)
DateEnd = DateSerial(2013, 8, 15)
If DateDiff("D", DateStart, Now) >= 0 And DateDiff("D", Now, DateEnd) >= 0 Then
If Hour(Now) >= 9 And Hour(Now) < 22 Then
'*** delete after debugging ***
MsgBox "play sound"
Set WshShell = CreateObject("WScript.Shell")
music = "C:\Users\MYUSERNAME\Desktop\MYSOUND.wav"
'*** 2nd parameter : 0 hides wmplayer, 1 shows it ***
WshShell.Run "wmplayer """ & music & """", 1, True
Else
'*** delete after debugging ***
MsgBox "Not the right time"
End If
Else
'*** delete after debugging ***
MsgBox "Not the right day"
End If
Also, if you want to debug a small script like this, you can call MsgBox to do a simple tracking of what's actually executed (in your example, replacing your "WScript.Quit 1" by MsgBox would show you that the date is not properly compared.
If I run following code in VBScript all works as expected, if i run it in ASP (IIS 7) the I get this wrong result. Does anyone know why?
mumber = "027609366"
WScript.Echo Left(number, 2) & " " & _
Mid(number, 3, 2) & " " & _
Mid(number, 5, 2) & " " & _
Right(number, 3)
' vbs => 03 76 09 366 (right)
' asp => 03 76 09 66 (wrong)
I now use the following which works:
Left(number, 2) & " " & _
Mid(number, 3, 2) & " " & _
Mid(number, 5, 2) & " " & _
Mid(number, 7)
But i wonder why this happens.
ASP code is written using VBScript, so the results can't be different, as they are technically the same thing.
Is there an extra space on the end of one of your numbers somewhere, that could be causing the number to be displayed incorrectly?