Simple date conversion to ASP Classic from ASP.Net - asp.net

How to do the same stuff using Classic ASP?
I mean, is it possible to do it in one row like in .Net?
string str1 = DateTime.Now.ToString("yyyyMMdd");
string str2 = DateTime.Now.ToString("H:mm:ss");

For yyyMMdd make it one row
<%= Year(Now) & Right("0" & Month(Now), 2) & right("0" & Day(Now), 2) %>
**OR**
<%= DatePart("yyyy",Now) & Right("0" & DatePart("m",Now), 2) & right("0" & DatePart("d",Now), 2) %>
For H:mm:ss
<%= Hour(Now) & ":" & Right("0" & Minute(Now), 2) & ":" & right("0" & Second(Now), 2) %>
**OR**
<%= DatePart("h",Now) & Right("0" & DatePart("n",Now), 2) & right("0" & DatePart("s",Now), 2) %>
You might look at this here briefly explain about date parsing in vbScript.
https://www.mikesdotnetting.com/Article/22/Date-formatting-in-VBScript

Related

Server Time using VBScript and ASP Classic

I created an application to show a press release once the target date and time have been reached and wanted to know if this is getting the time from the server or the client because I'd like to use the server's time so that someone doesn't just change their clock in order to see it.
Here's my code:
<%
dim strDate
dim strTime
dim strTarget_time
dim strTarget_date
dim strBreak
dim strRuleBreak
dim strToday
strDate = Date()
strTime = Time()
strright_now = Now()
strTarget_time = "3:27:00 PM"
strTarget_date = "6/26/2012"
strBreak = "<br />"
strRuleBreak = "<br /><hr><br />"
strToday = Now()
response.write("<h2>TEST VARIABLES</h2>")
response.write("<p><strong>Today's date:</strong> " & strDate & strBreak)
response.write("<strong>Current time:</strong> " & strTime & strBreak)
response.write("<strong>Target date:</strong> " & strTarget_date & strBreak)
response.write("<strong>Target time:</strong> " & strTarget_time & "</p>")
response.write(strRuleBreak)
'TIME TESTER
response.write("<h2>TIME TESTER</h2>")
response.write("<p><nobr>Testing to see if it is past the target time of: " & strTarget_time & "</nobr></p>")
if strTime >= cdate(strTarget_time) then
response.write("<p>Yes, it is now " & Now() & ", which <strong>IS</strong> past the target time of: " & strTarget_time & "</p>")
else
response.write("<p>No, it is now " & Now() & ", which is <strong>NOT</strong> past the target time of: " & strTarget_time & "</p>")
end if
response.write(strRuleBreak)
'DATE TESTER
response.write("<h2>DATE TESTER</h2>")
response.write("<p><nobr>Testing to see if it is past the target date of: " & strTarget_date & "</nobr></p>")
if strToday >= cdate(strTarget_date) then
response.write("<p>Yes, it is now " & Now() & ", which <strong>IS</strong> past the target date of: " & strTarget_date & "</p>")
else
response.write("<p>No, it is now " & Now() & ", which is <strong>NOT</strong> past the target date of: " & strTarget_date & "</p>")
end if
response.write(strRuleBreak)
'DATE AND TIME TESTER
response.write("<h2>DATE AND TIME TESTER</h2>")
response.write("<p><nobr>Testing to see if it is past the target of: " & strTarget_date & " - " & strTarget_time & "</nobr></p>" & strBreak)
if strToday >= cdate(strTarget_date) AND strTime >= cdate(strTarget_time) then
response.write("<p>Yes, it is now " & Now() & ", which <strong>IS</strong> past the target of: " & strTarget_date & " - " & strTarget_time & "</p>")
else
response.write("<p>No, it is now " & Now() & ", which is <strong>NOT</strong> past the target of: " & strTarget_date & " - " & strTarget_time & "</p>")
end if
response.write(strRuleBreak)
%>
So in this case, if the time and date is AFTER 6/26/2012 3:27 PM, then the section will show. I'm mainly asking because I want to clarify whether this is client or server side time being used.
This is going to be server-side, as that is where ASP code is executed. In order to get the client-side datetime, you would need to use a script to run in the browser - generally JavaScript.

Classic ASP: Empty Select When Using If Statement

I have a simple bit of code that is filling a select object from a db table. I want to check a value to set an entry as selected, but when I check a db value, I get an empty select box. This code produces the empty box:
response.write "<td><select name='FromDept'>"
Do While not rs.eof
If rs("DeptID") = 61 Then
response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName") & "</option>"
Else
response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
End If
rs.MoveNext
Loop
rs.close
response.write "</select></td>"
However, this code produces a select box with values:
response.write "<td><select name='FromDept'>"
LpCnt = 0
Do While not rs.eof
If LpCnt = 9 Then
response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName") & "</option>"
Else
response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
End If
rs.MoveNext
LpCnt = LpCnt + 1
Loop
rs.close
response.write "</select></td>"
Thanks for any help!
Assign the value to a temporary variable:
response.write "<td><select name='FromDept'>"
Do While not rs.eof
dept = rs("DeptID")
If dept = 61 Then
response.write "<option value=" & dept & " selected>" & rs("DeptName") & "</option>"
...
Empty drop down means you get error in that statement and it's ignored most likely due to On Error Resume Next line you have somewhere.
First of all, get rid of that On Error Resume Next line.
Second, error in such code means type conversion problem. Try this code instead:
Dim curDeptID
Do While not rs.eof
curDeptID = 0
If Not IsNull(rs("DeptID")) Then
curDeptID = CLng(CStr(rs("DeptID")))
End If
If curDeptID=61 Then
response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName") & "</option>"
Else
response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
End If
rs.MoveNext
Loop

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

Resources