How to transform a date-string in classic asp - asp-classic

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

Related

Telerik AsyncUpload trying to rename file get IOException

i want to rename my file. But i got a IOException. It says "The Process Cannot Access the File Because It Is Being Used by Another Process".
This is my Code:
asp.net:
<telerik:RadAsyncUpload ID="rauKachelUpload" runat="server" ChunkSize="0" Localization-Cancel="Löschen" Localization-Remove="Entfernen" Localization-Select="Auswählen"
Culture="de-DE" Skin="MetroTouch" MaxFileInputsCount="1" OnFileUploaded="rauKachelUpload_FileUploaded">
</telerik:RadAsyncUpload>
vb.net:
Protected Sub rauKachelUpload_FileUploaded(sender As Object, e As FileUploadedEventArgs)
Try
Using fileStream As Stream = e.File.InputStream
Using img As System.Drawing.Image = System.Drawing.Image.FromStream(fileStream)
Dim h As Integer = img.Height
Dim w As Integer = img.Width
img.Dispose()
Dim fileName As String = e.File.GetName()
If w = MaxWidth And h = MaxHeight Then
rauKachelUpload.TargetFolder = "img/kachel_grafik"
Dim TimeStamp As String = DateDiff("s", "01/1/1970 12:00:00 AM", DateTime.Now)
fileName = "KI_" & TimeStamp & WelcheSparteUndGröße
KachelPfad = "~/img/kachel_grafik/" & fileName
Else
KachelFalsch = True
End If
If KachelFalsch = False Then
e.File.SaveAs(fileName)
Page.ClientScript.RegisterClientScriptBlock([GetType](), "CloseScript", "redirectParentPage('VermittlerBearbeiten.aspx?ID=" & VermittlerID & "&KBFN=" & KachelPfad & "&NA=true" & "&fwg=" & WelcheSparteUndGröße & "&Ang1=" & hfAng1CHK.Value & "&Ang2=" & hfAng2CHK.Value & "&Ang3=" & hfAng3CHK.Value & "&Ang4=" & hfAng4CHK.Value & "&AngSrc1=" & hfKachelIMGSrcBaufi.Value & "&AngSrc2=" & hfKachelIMGSrcImmo.Value & "&AngSrc3=" & hfKachelIMGSrcPhoto.Value & "&AngSrc4=" & hfKachelIMGSrcAsse.Value & "');", True)
Else
rnfIconNichtErzeugt.Visible = True
End If
End Using
End Using
Catch ex As Exception
rnfIconNichtErzeugt.Visible = True
End Try
End Sub
Without trying to rename my file, it works fine.
Does anyone has an Idea what i did wrong?
Thanks for reading.
Daniel
You need to make sure that the fileStream is properly closed before attempting to rename the file since the particular file will be held by the fileStream object. You can try fileStream.Close() after the img.Dispose() statement.

Setting up Google weather extended forecast with classic ASP

Just a little background, our marketing department has been using static weather images on their signage and asked if it would be possible to pull something that updated in realtime. Since I'm the only person in our department with any type of programming experience, and I'm a novice at best, I got asked to come up with a solution. I have worked with classic asp in the past for a couple of other small projects I've done, so I decided to go that route. I have everything working the way I want it to with the exception of the extended forecast data. In the sample below I have the variables that I want to display with a For statement to pull the extended forecast xml data for each day. But for some reason it is only pulling the the last day of the extended forecast.
If (objXMLDOM.parseError.errorCode <> 0) Then
Response.Write("<p>Error parsing XML: " & objXMLDOM.parseError.reason & "</p>")
Else
For i = 0 to 3
Set objFuture = objXMLDOM.getElementsByTagName("forecast_conditions").Item(i) 'pull the XML node for each from one to three
For Each xmlNode In objFuture.childNodes 'loop through the dom tree
If (xmlNode.nodeName = "icon") Then
strIcon1 = "<img src=""http://www.google.com" & xmlNode.Attributes.getNamedItem("data").text & """ border=""0"">" & vbCrLf
End If
If (xmlNode.nodeName = "condition") Then
strCondition1 = xmlNode.Attributes.getNamedItem("data").text & vbCrLf
End If
If (xmlNode.nodeName = "low") Then
strLow = xmlNode.Attributes.getNamedItem("data").text & vbCrLf
End If
If (xmlNode.nodeName = "high") Then
strHigh = xmlNode.Attributes.getNamedItem("data").text & vbCrLf
End If
If (xmlNode.nodeName = "day_of_week") Then
strDay = xmlNode.Attributes.getNamedItem("data").text & vbCrLf
End If
Set objFuture = nothing
Next
Next
End if
I am then outputing the 4 day forecast into my main page with the following code:
<%For Each Item in objXMLDOM.getElementsByTagName("forecast_conditions")
Response.Write ("<td>" & strDay & "<br>" & strIcon1 & "<br>" & strCondition1 & "<br>" & "Low:&nbsp" & strLow & "°F" & "<br>" & "High:&nbsp" & strHigh & "°F" & "<br>" & "</td>")
Next %>
I have been looking at this off and on for a couple of days now and just can't seem to find what I am missing to pull each day instead of the just the last one. If anyone has any suggestions I would appreciate it!
You are using the same set of variables to store the forecast data for each day. So during every iteration of your for loop you are overwriting the previous day's information.
The solution is to move the code that writes the data to the screen insideyour first for loop, like such:
If (objXMLDOM.parseError.errorCode <> 0) Then
Response.Write("<p>Error parsing XML: " & objXMLDOM.parseError.reason & "</p>")
Else
For i = 0 to 3
Set objFuture = objXMLDOM.getElementsByTagName("forecast_conditions").Item(i)
For Each xmlNode In objFuture.childNodes
...
Next
' Write to the page here
Response.Write ("<td>" & strDay & "<br>" & strIcon1 & "<br>" & strCondition1 & "<br>" & "Low:&nbsp" & strLow & "°F" & "<br>" & "High:&nbsp" & strHigh & "°F" & "<br>" & "</td>")
Next
End if

VBScript ISO8601

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

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

Using & and + in asp.net with vb

I have some confusion as to the use of + and & in ASP.NET and VB.NET. See the following code:
Dim dtUser As DataTable = GetDetails()
Dim serverPath As String = Nothing
Dim virtualServerPath As String = Nothing
Dim parentDir As DirectoryInfo = Nothing
Dim childDir As DirectoryInfo = Nothing
serverPath = Page.Server.MapPath(".") + "\"
virtualServerPath = serverPath.Substring(0, serverPath.LastIndexOf("\"))
virtualServerPath = virtualServerPath + "\SiteImages\" + dtUser.Rows(0)("Name")
parentDir = Directory.CreateDirectory(virtualServerPath)
childDir = parentDir.CreateSubdirectory(Session("RegID"))
Dim strUserName as String=dtUser.Rows(0)("Name")
If flUpload.HasFile Then
flUpload.SaveAs(Server.MapPath("~/SiteImages/" & dtUser.Rows(0)("Name") & "/" & childDir + "/" + flUpload.FileName))
I am getting error concerned with usage of + and & in
flUpload.SaveAs(Server.MapPath("~/SiteImages/" & strUserName & "/" & childDir + "/" + flUpload.FileName))
Can anybody help to remove the error
Use "&" for concatenation, "+" will work until you have a value that a mathematical operation can be performed on in the concatenation. It will attempt to perform the addition rather than concatenation.
eg.
"blah" & "blah" works
"blah" + "blah" works
"blah" & 5 works
"blah" + 5 fails
The last one does not work as it will try to "add" 5 and a string
change
flUpload.SaveAs(Server.MapPath("~/SiteImages/" &
dtUser.Rows(0)("Name") &
"/" &
childDir +
"/" +
flUpload.FileName))
to
flUpload.SaveAs(Server.MapPath("~/SiteImages/" &
dtUser.Rows(0)("Name") &
"/" &
childDir &
"/" &
flUpload.FileName))
and pay heed to #CodeWiki's comment on this answer that is not to mix + and & in one statement.
In VB, the & is for string concatenation. You should only use + for addition operations.
The problem with + is that it tries to do implicit conversion so you can do
2.5 + 5
C# would give you an error because 2.5 would be a float and 5 would be an int. You would need to cast them. VB does the casting implicitly which can hide some bugs.
As far as I know VB string concatenation uses &, don't use +
"A" & "B" & "C" = "ABC"
"A" + "B" + "C" = hmmm error? (Edit) apparently this works...
(More Edit)...
Possible answer to your error:
I don't think the error is anything to do with & or + now.
It might be your childDir which is of type DirectoryInfo. You might want to get the name of the directory in it instead of just plopping childDir in the string concat.
try change it to & childDir.Name in that concat.

Resources