Easy solution?

T

the other john

I'm hoping for an easy solution to this that I'm not seeing that
someone more experienced will.

All I want to do it mark an option tag as selected....

<%
hourCounter = 1
Do WHILE hourCounter <= 12 %>
<option value="<%=hourCounter%>" <%If hourCounter =
hour(rsCalendarEdit("fld_calendar_DateTime")) Then Response.write "
selected" End If %>><%=hourCounter%></option>
<% hourCounter = hourCounter + 1
Loop
%>

The trouble is if the time is "PM" I have a problem as you can see. Is
there a method or property here I can implement that I'm not seeing?

Thanks!
 
R

Ray Costanzo [MVP]

Where's PM coming in? Is your data stored as actual date/time data? Try
simplifying the code a little bit, at least as far as readability and fewer
reads of object data.
<%

Dim iSelectedHour
iSelectedHour = CInt(Hour(rsCalender.Fields.Item("fld_calendar_DateTime")))

hourCounter = 1
Do while hourCounter <= 12

%>
<option value="<%=hourCounter%>"<% If hourCounter = iSelectedHour Then
Response.Write " selected"%>><%=hourCounter%></option>
<%
hourCounter = hourCounter + 1
Loop
%>


Although, this doesn't really change much. Can you explain what PM problem
you're having?


Ray at home
 
M

McKirahan

the other john said:
I'm hoping for an easy solution to this that I'm not seeing that
someone more experienced will.

All I want to do it mark an option tag as selected....

<%
hourCounter = 1
Do WHILE hourCounter <= 12 %>
<option value="<%=hourCounter%>" <%If hourCounter =
hour(rsCalendarEdit("fld_calendar_DateTime")) Then Response.write "
selected" End If %>><%=hourCounter%></option>
<% hourCounter = hourCounter + 1
Loop
%>

The trouble is if the time is "PM" I have a problem as you can see. Is
there a method or property here I can implement that I'm not seeing?

Thanks!

How is "PM" a problem?

Hour() =
"Returns a whole number between 0 and 23,
inclusive, representing the hour of the day."

Thus, modifying your code, this should work;
(presuming your field is DateTime field):

<form>
<select>
<%
Dim hourCounter
hourCounter = 0
Dim hourRecord
hourRecord = Hour(rsCalendarEdit("fld_calendar_DateTime"))
Do While hourCounter < 24
%>
<option value="<%=hourCounter%>"
<%
If hourCounter = hourRecord Then
Response.Write " selected"
End If
%><%=hourCounter%>
</option>
<% hourCounter = hourCounter + 1
Loop
%>
</select>
</form>

Or do you not want to use military time?
 
T

the other john

The trouble I'm having with PM is that the client chooses their time
via a dropbox. The script it intended for an edit page where the time
already exists in the database. The trouble with PM is when the time
is PM this script is looking for "6" but the time in the database is
"18".
 
M

McKirahan

the other john said:
The trouble I'm having with PM is that the client chooses their time
via a dropbox. The script it intended for an edit page where the time
already exists in the database. The trouble with PM is when the time
is PM this script is looking for "6" but the time in the database is
"18".

Try this:

<form>
<select>
<%
Dim hourArray(23)
hourArray(0) = "12:00 AM"
hourArray(1) = "1:00 AM"
hourArray(2) = "2:00 AM"
hourArray(3) = "3:00 AM"
hourArray(4) = "4:00 AM"
hourArray(5) = "5:00 AM"
hourArray(6) = "6:00 AM"
hourArray(7) = "7:00 AM"
hourArray(8) = "8:00 AM"
hourArray(9) = "9:00 AM"
hourArray(10) = "10:00 AM"
hourArray(11) = "11:00 AM"
hourArray(12) = "12:00 PM"
hourArray(13) = "1:00 PM"
hourArray(14) = "2:00 PM"
hourArray(15) = "3:00 PM"
hourArray(16) = "4:00 PM"
hourArray(17) = "5:00 PM"
hourArray(18) = "6:00 PM"
hourArray(19) = "7:00 PM"
hourArray(20) = "8:00 PM"
hourArray(21) = "9:00 PM"
hourArray(22) = "10:00 PM"
hourArray(23) = "11:00 PM"
Dim hourCounter
hourCounter = 0
For hourCounter = 0 To UBound(hourArray)
%>
<option value="<%=hourCounter%>"
<% If hourCounter = rsCalendarEdit("fld_calendar_DateTime") Then
Response.Write " selected"
End If
%><%=hourArray(hourCounter)%>
</option>
<%
Next
%>
</select>
</form>
 
D

dNagel

this is an example I laid out the other day... might be useful...

heres the output :

You are going to experience problems with this method unless you format your
Date from SQL in an acceptable format ( as presented below ) before it hits
your JS code.

01-24-2006 13:30:00

Tue Jan 24 13:30:00 PST 2006
1:30 PM
The time is now : 8:32 AM



D.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<pre>
You are going to experience problems with this method unless you format your
Date from SQL in an acceptable format ( as presented below ) before it hits
your JS code.

01-24-2006 13:30:00
</pre>

<script>

Date.prototype.humanTime = function () {
var sDate = new String(), sHour = new String(), sAMPM = new String();
sHour = this.getHours() % 12 ;
sAMPM = parseInt(this.getHours()) > 11 ? ' PM' : ' AM';
if (sHour == 0) sHour = 12;
sDate = sHour + ':' + this.getMinutes() + sAMPM;
return (sDate.toString());
}
var someDate= new Date('01-24-2006 13:30:00');
document.write ( '<pre>' + someDate.toString() + '<br />')
document.write ( someDate.humanTime() + '<br />')

var sNow = new Date()
document.write ( 'The time is now : ' + sNow.humanTime() )

document.write ( '</pre>' )

</script>
</body>
</html>
 
T

the other john

What I actually need just the hour number. I was hoping the following
would work but it doesn't, any idea why?
<%
hourCounter = 1
Do WHILE hourCounter <= 12 %>
<option value="<%=hourCounter%>" <%If hourCounter =
hour(rsCalendarEdit("fld_calendar_DateTime")) OR hourCounter =
hour(rsCalendarEdit("fld_calendar_DateTime")) + 12 Then Response.write
" selected" End If %>><%=hourCounter%></option>
<% hourCounter = hourCounter + 1
Loop
%>
Thanks again all!
 
M

McKirahan

the other john said:
What I actually need just the hour number. I was hoping the following
would work but it doesn't, any idea why?
<%
hourCounter = 1
Do WHILE hourCounter <= 12 %>
<option value="<%=hourCounter%>" <%If hourCounter =
hour(rsCalendarEdit("fld_calendar_DateTime")) OR hourCounter =
hour(rsCalendarEdit("fld_calendar_DateTime")) + 12 Then Response.write
" selected" End If %>><%=hourCounter%></option>
<% hourCounter = hourCounter + 1
Loop
%>
Thanks again all!

Try " - 12" instead of " + 12" as Hour() returns 0 to 23.
 
M

McKirahan

the other john said:
ah HA, success! But I don't understand why. - rather than + ...I'm
cOnFuSeD

I like to separate logic from display so here's a rewrite of your code:

<%
Option Explicit
Dim hourOp
hourOp = ""
Dim hourIf
hourIf = Hour(rsCalendarEdit("fld_calendar_DateTime"))
If hourIf = 0 Then hourIf = 24
If hourIf > 12 Then hourIf = hourIf - 12
Dim hourIs
For hourIs = 1 To 12
hourOp = hourOp & "<option value='" & hourIs & "'"
If hourIs = hourIf Then hourOp = hourOp & " selected"
hourOp = hourOp & "></option>" & vbCrLf
Next
%>

<%=hourOp%>

From the above:
a) "hourIs" (your "hourCounter") goes from 0 to 12.
b) "hourIf" (from your recordset) may be 0 to 23.
c) "hourOp" is all of the "<option>" tags.

Thus, to compare "hourIs" to "hourIf", you'll have
to subtract 12 from "hourIf" when it's greater than 12.

But, what was handled before, was when "hourIf" was 0.
Now, since 0 is Midnight, I change it to 24.

Does that help you understand?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top