Easy solution?

Discussion in 'ASP General' started by the other john, Feb 8, 2006.

  1. 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!
     
    the other john, Feb 8, 2006
    #1
    1. Advertising

  2. 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




    "the other john" <> wrote in message
    news:...
    > 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!
    >
     
    Ray Costanzo [MVP], Feb 8, 2006
    #2
    1. Advertising

  3. the other john

    McKirahan Guest

    "the other john" <> wrote in message
    news:...
    > 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?
     
    McKirahan, Feb 8, 2006
    #3
  4. 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".
     
    the other john, Feb 8, 2006
    #4
  5. the other john

    McKirahan Guest

    "the other john" <> wrote in message
    news:...
    > 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>
     
    McKirahan, Feb 8, 2006
    #5
  6. the other john

    dNagel Guest

    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>

    the other john wrote:
    > 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".
    >
     
    dNagel, Feb 8, 2006
    #6
  7. 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!
     
    the other john, Feb 9, 2006
    #7
  8. the other john

    McKirahan Guest

    "the other john" <> wrote in message
    news:...
    > 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.
     
    McKirahan, Feb 9, 2006
    #8
  9. ah HA, success! But I don't understand why. - rather than + ...I'm
    cOnFuSeD
     
    the other john, Feb 10, 2006
    #9
  10. the other john

    McKirahan Guest

    "the other john" <> wrote in message
    news:...
    > 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?
     
    McKirahan, Feb 10, 2006
    #10
  11. the other john

    McKirahan Guest

    "McKirahan" <> wrote in message
    news:...

    [snip]

    Oops.

    > For hourIs = 1 To 12


    > a) "hourIs" (your "hourCounter") goes from 0 to 12.


    should be

    a) "hourIs" (your "hourCounter") goes from 1 to 12.
     
    McKirahan, Feb 10, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hazzard
    Replies:
    2
    Views:
    651
    Hazzard
    Apr 6, 2004
  2. Kai Henry

    Codebehind Problem -> Easy solution...

    Kai Henry, Apr 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    294
    Kai Henry
    Apr 12, 2004
  3. Andrew Francis
    Replies:
    0
    Views:
    426
    Andrew Francis
    Jun 28, 2006
  4. Bruno Desthuilliers
    Replies:
    5
    Views:
    395
    Bruno Desthuilliers
    Aug 29, 2007
  5. howa
    Replies:
    1
    Views:
    109
    smallpond
    Mar 14, 2009
Loading...

Share This Page