VBScript ISO8601 - datetime

In VBScript, does FormatDateTime have ISO 8601 support?
If not, how would I write such function with it?
For example:
Response.Write FormatAsISO8601(#05/04/2011#)
Function FormatAsISO8601(datetime)
...
End Function

Here is the specific code I needed from Chris' class, a bit more optimized:
Public Function ToIsoDateTime(datetime)
ToIsoDateTime = ToIsoDate(datetime) & "T" & ToIsoTime(datetime) & CurrentTimezone
End Function
Public Function ToIsoDate(datetime)
ToIsoDate = CStr(Year(datetime)) & "-" & StrN2(Month(datetime)) & "-" & StrN2(Day(datetime))
End Function
Public Function ToIsoTime(datetime)
ToIsoTime = StrN2(Hour(datetime)) & ":" & StrN2(Minute(datetime)) & ":" & StrN2(Second(datetime))
End Function
Private Function StrN2(n)
If Len(CStr(n)) < 2 Then StrN2 = "0" & n Else StrN2 = n
End Function

Here's a brute force function:
sDate = iso8601Date(Now)
msgbox sDate
Function iso8601Date(dt)
s = datepart("yyyy",dt)
s = s & RIGHT("0" & datepart("m",dt),2)
s = s & RIGHT("0" & datepart("d",dt),2)
s = s & "T"
s = s & RIGHT("0" & datepart("h",dt),2)
s = s & RIGHT("0" & datepart("n",dt),2)
s = s & RIGHT("0" & datepart("s",dt),2)
iso8601Date = s
End Function

Not without loading some COM component as far as I know.
Here's a VBScript class that someone wrote.

Some corrections
Function iso8601Date(dt)
s = datepart("yyyy",dt)
s = s & "-" & RIGHT("0" & datepart("m",dt),2)
s = s & "-" & RIGHT("0" & datepart("d",dt),2)
s = s & "T"
s = s & RIGHT("0" & datepart("h",dt),2)
s = s & ":" & RIGHT("0" & datepart("n",dt),2)
s = s & ":" & RIGHT("0" & datepart("s",dt),2)
iso8601Date = s
End Function

Related

Ajax VB Json string

I am having difficulties finding a Visual Basic Json string that will work for this instance. I keep getting an "Error Message Status" of "[ object Object ]".
I got this same one to work in C# with "\"" in the Json code but VB is not so easy.
Default.aspx
<script type="text/javascript" src="~/jquery=1.10.2.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#<%= Button1.ClientID %>").click(function () {
var mo1 = $("#<%= TextBox1.ClientID %>").val();
var dy1 = $("#<%= TextBox2.ClientID %>").val();
var yr1 = $("#<%= TextBox3.ClientID %>").val();
var data = { mo: mo1, dy: dy1, yr: yr1 };
var json1 = JSON.stringify(data);
$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: json1,
url: "Default.aspx/GetDate",
success: function (result) {
$("#<%= TextBox4.ClientID %>").val(result.d);
$("#<%= Button2.ClientID %>").trigger(click);
},
error: function (status, ex) {
alert("Error Code: Status: " + status + " Ex: " + ex);
}
});
return false;
});
});
</script>
Default.aspx.vb
<System.Web.Services.WebMethod> Public Shared Function GetDate(ByVal mo As Integer, ByVal dy As Integer, ByVal yr As Integer) As String
...
json1 = "[{""id"":""100"",""datetime"":""04/10/2017"",""col1"":""1"",""col2"":""2"",""col3"":""3""}]"
Dim jsonout As String = JsonConvert.SerializeObject(json1)
Return jsonout
End Function
I tried others:
'json1 = "[{" & Chr(34) & "id" & Chr(34) & ":" & Chr(34) & dataa(i, 1) & Chr(34) & "," & Chr(34) & "datetime" & Chr(34) & ":" & Chr(34) & dataa(i, 2) & Chr(34) & "," & Chr(34) & "col1" & Chr(34) & ":" & Chr(34) & dataa(i, 3) & Chr(34) & "," & Chr(34) & "col2" & Chr(34) & ":" & Chr(34) & dataa(i, 4) & Chr(34) & "," & Chr(34) & "col3" & Chr(34) & ":" & Chr(34) & dataa(i, 5) & Chr(34) & "}]"
'json1 = "[{""id"":""" & dataa(i, 1) & """,""datetime"":""" & dataa(i, 2) & """,""col1"":""" & dataa(i, 3) & """,""col2"":""" & dataa(i, 4) & """col3"","":""" & dataa(i, 5) & """}]"
'json1 = "[{" & """id"":"" & dataa(i, 1) & "",""datetime"":"" & dataa(i, 2) & ""col1"":"" & dataa(i, 3) & "",""col2"":"" & dataa(i, 4) & "",""col3"":"" & dataa(i, 5) & ""}]"
'json1 = "[{" & """id""" & ":""" & dataa(i, 1) & """," & """datetime""" & ":""" & dataa(i, 2) & """," & """col1""" & ":""" & dataa(i, 3) & """," & """col2""" & ":""" & dataa(i, 4) & """," & """col3""" & ":""" & dataa(i, 5) & """}]"
'json1 = "[{" & """ & "id" & """ & ":" & """ & "100" & """ & "," & """ & "datetime" & """ & ":" & """ & "04/10/2017" & """ & "," & """ & "col1" & """ & ":" & """ & "1" & """ & "," & """ & "col2" & """ & ":" & """ & "2" & """ & "," & """ & "col3" & """ & ":" & """ & "3" & """ & "}]"
Class.vb
Public Class datapart
Public Property id() As Integer
Get
Return m_id
End Get
Set(value As Integer)
m_id = value
End Set
End Property
Private m_id As Integer
Public Property datetime() As String
Get
Return m_datetime
End Get
Set(value As String)
m_datetime = value
End Set
End Property
Private m_datetime As String
Public Property col1() As Integer
Get
Return m_col1
End Get
Set(value As Integer)
m_col1 = value
End Set
End Property
Private m_col1 As Integer
Public Property col2() As Integer
Get
Return m_col2
End Get
Set(value As Integer)
m_col2 = value
End Set
End Property
Private m_col2 As Integer
Public Property col3() As Integer
Get
Return m_col3
End Get
Set(value As Integer)
m_col3 = value
End Set
End Property
Private m_col3 As Integer
End Class

Internal Server Error 500 w/ IIS Log

I am getting some errors on my classic asp website. When the website runs for the first time it sometimes does an Internal Server Error. Then a refresh would fix it. I decided to check my IIS logs to see what the problem is but i can't interpret it. Here is the log line
2013-12-09 15:29:00 xx.xx.xx.xx GET / |37|80070005|Access_is_denied.__ 80 - xx.xxx.xx.xx Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/31.0.1650.63+Safari/537.36 500 0 0 702
How about setup custom pages for handle 500 and 500 100 errors?
Create some folder, let's say D:\InetPub\Web01\Err\
Add IUSR_Web01 user with write permission
In IIS for Web01 web site (sample for IIS 6.0)
Put following code in file 500.asp and 500100.asp
Option Explicit
Response.Buffer = True
Response.Expires = -1
Response.ExpiresAbsolute = #Jan 31,2000 12:30:00#
Response.Clear
Dim FS, TF, N, ASPErr
N = Now
Set ASPErr = Server.GetLastError()
Set FS = CreateObject ("Scripting.FileSystemObject")
Set TF = FS.CreateTextFile ("D:\InetPub\1click.lv\Err\500 " & "Error" & Right ("0" & Year (N), 4) & Right ("0" & Month (N), 2) & Right ("0" & Day (N), 2) & "_" & Right ("0" & Hour (N), 2) & Right ("0" & Minute (N), 2) & Right ("0" & Second (N), 2) & ".txt", True, False)
TF.Write MyErrorInfo (ASPErr, False, False, "1click.lv", "")
TF.Close
Set FS = Nothing
Response.Write MyErrorInfo (ASPErr, True, True, "1click.lv", "zam#1click.lv")
Err.Clear
The function:
Function MyErrorInfo (ASPErr, AsHTML, ShowContactInfo, WebTitle, AdminEmail)
Dim Result
Result = ""
If AsHTML = True Then
Result = Result & "<html><head><title>Error occur</title></head><body><font face=Verdana size=2>"
If (ShowContactInfo = True) Then
Result = Result & "<p align=center>"
Result = Result & "<font size=4>"
Result = Result & "<font color=""#008000"">" & WebTitle & "</font><br>"
Result = Result & "<font color=""#800000"">500 Error occur</font><br>"
Result = Result & "Please contact us by email at " & AdminEmail & " and inform about this error<br><br>"
Result = Result & "Thank you for your support!"
Result = Result & "</font>"
Result = Result & "</p>"
End If
Result = Result & "<hr>"
Result = Result & "Error number: <b>" & ASPErr.Number & "</b><br>"
Result = Result & "Error source: <b>" & ASPErr.Source & "</b><br>"
Result = Result & "Error description: <b>" & ASPErr.Description & "</b><br>"
Result = Result & "Error line: <b>" & ASPErr.Line & "</b><br>"
Result = Result & "Client IP: <b>" & Request.ServerVariables ("REMOTE_ADDR") & "</b><br>"
Result = Result & "Client Browser: <b>" & Request.ServerVariables ("HTTP_USER_AGENT") & "</b><br>"
Result = Result & "Client Referer: <b>" & Request.ServerVariables ("HTTP_REFERER") & "</b><br>"
Result = Result & "Path: <b>" & Request.ServerVariables ("PATH_INFO") & "</b><br>"
Result = Result & "Request method: <b>" & Request.ServerVariables ("REQUEST_METHOD") & "</b><br>"
Result = Result & "Request FORM: <b>" & Request.Form & "</b><br>"
Result = Result & "Request QUERY: <b>" & Request.QueryString & "</b><br>"
Result = Result & "<hr>"
Result = Result & "</font></body></html>"
Else
Result = Result & WebTitle & vbCrLf
Result = Result & "Error number: " & ASPErr.Number & vbCrLf
Result = Result & "Error source: " & ASPErr.Source & vbCrLf
Result = Result & "Error description: " & ASPErr.Description & vbCrLf
Result = Result & "Error line: " & ASPErr.Line & vbCrLf
Result = Result & "Client IP: " & Request.ServerVariables ("REMOTE_ADDR") & vbCrLf
Result = Result & "Client Browser: " & Request.ServerVariables ("HTTP_USER_AGENT") & vbCrLf
Result = Result & "Client Referer: " & Request.ServerVariables ("HTTP_REFERER") & vbCrLf
Result = Result & "Path: " & Request.ServerVariables ("PATH_INFO") & vbCrLf
Result = Result & "Request method: " & Request.ServerVariables ("REQUEST_METHOD") & vbCrLf
Result = Result & "Request FORM: " & Request.Form & vbCrLf
Result = Result & "Request QUERY: " & Request.QueryString & vbCrLf
End If
MyErrorInfo = Result
End Function

How do I create XML's dateTime format using vbscript?

I need to create the date and time of extraction in the following format.
extractDate='2011-10-18T12:00:00.000000'
What is the best way, using vbscript / asp3 to do this? I was just thinking of hard-coding the end portion of .000000 since this is not a horse race LOL
Thanks.
So Far I am starting with something like;
xmlDateTime = FormatDateTime(Now(),0)
xmlDateTime = Replace(xmlDateTime, " PM", "")
xmlDateTime = Replace(xmlDateTime, " AM", "")
Response.Write xmlDateTime & "<br>"
UPDATE:
My potential solution:
xmlDateTime = FormatDateTime(Now(),0)
xmlDateTime = Replace(xmlDateTime, " PM", "")
xmlDateTime = Replace(xmlDateTime, " AM", "")
Response.Write xmlDateTime & "<br>"
splitDateTime = Split(xmlDateTime, " ")
xmlDate = splitDateTime(0)
xmlTime = splitDateTime(1)
strYear = DatePart("yyyy",xmlDate)
strMonth = DatePart("m",xmlDate)
strDay = DatePart("d",xmlDate)
Response.Write strYear & "<br>"
Response.Write strMonth & "<br>"
Response.Write strDay & "<br>"
xmlDateTime = strYear & "-" & strMonth & "-" & strDay & "T" & xmlTime & ".000000"
Response.write xmlDateTime & "<br>"
You need to construct the date string manually.
Function dd(str)
dd = Right("0" & str, 2)
End Function
d = Now
extractDate = Year(d) & "-" & dd(Month(d)) & "-" & dd(Day(d)) & "T" _
& dd(Hour(d)) & ":" & dd(Minute(d)) & ":" & dd(Second(d)) & ".000000"

How can I have YYYY-MM-DD date format in asp classic?

Im writing an small code and I need to add tomorrow's date on it. The format should be YYYY-MM-DD and I already tried " DateAdd("d",1,d_now) and it returns MM-DD-YYYY. How can I format this to YYYY-MM-DD ?
format is not enough....
'format a number with the correct amount of digits
eg: 9 becomes 09 but 11 stays 11'
Function formatNumber(value, digits)
if digits > len(value) then
formatNumber = String(digits-len(value),"0") & value
else
formatNumber = value
end if
End Function
'write the date "manually"'
response.write Year(date) & "-" & _
formatNumber(Month(date),2) & _
"-" & formatNumber(Day(date),2)
Take a look at this post - it might help you out... How to transform a date-string in classic asp
I found the answer. This is the easiest way to do that.
d_now = date()
d = split(DateAdd("d",1,d_now),"/")
s = d(2) & "-" & d(0) & "-" & d(1)
here is one more way to do it
function FormatMyDate(myDate)
FormatMyDate = Year(myDate) & "-" & _
Right("0" & Month(myDate), 2) & "-" & _
Right("0" & Day(myDate), 2)
end function
Build your YYYY-MM-DD using this routine:
strMonth = Month(Date)
strDay = Day(Date)
if len(strMonth) < 2 then
strMonth = "0" & strMonth
end if
if len(strDay) < 2 then
strDay = "0" & strDay
end if
strDate = Year(Date) & "-" & strMonth & "-" & strDay

How to transform a date-string in classic asp

I'm a little blockheaded right now…
I have a date string in european format dd.mm.yyyy and need to transform it to mm.dd.yyyy with classic ASP. Any quick ideas?
If its always in that format you could use split
d = split(".","dd.mm.yyyy")
s = d(1) & "." & d(0) & "." & d(2)
this would allow for dates like 1.2.99 as well
Dim arrParts() As String
Dim theDate As Date
arrParts = Split(strOldFormat, ".")
theDate = DateTime.DateSerial(parts(2), parts(1), parts(0))
strNewFormat = Format(theDate, "mm.dd.yyyy")
OK, I just found a solution myself:
payment_date = MID(payment_date,4,3) & LEFT(payment_date,3) & MID(payment_date,7)
This is a way to do it with built in sanity check for dates:
Dim OldString, NewString
OldString = "31.12.2008"
Dim myRegExp
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.]((19|20)[0-9]{2})"
If myRegExp.Test Then
NewString = myRegExp.Replace(OldString, "$2.$1.$3")
Else
' A date of for instance 32 December would end up here
NewString = "Invalid date"
End If
I have my own date manipulation functions which I use in all my apps, but it was originally based on this sample:
http://www.adopenstatic.com/resources/code/formatdate.asp
function MyDateFormat(mydate)
'format: YYYYMMDDHHMMSS
MyDateFormat = year(mydate) & right("0" & month(mydate),2) & _
right("0" & day(mydate),2) & right("0" & hour(mydate),2) &_
right("0" & minute(mydate),2) & right("0" & second(mydate),2)
end function
response.write(MyDateFormat(Now))
show: 20200623102805

Resources