Connecting Tech Pros Worldwide Forums | Help | Site Map

use PC time in a dropdown list

Newbie
 
Join Date: Jan 2009
Location: TX
Posts: 16
#1: May 22 '09
Hi, I need some help with time. I have a dropdown list that shows various times so the user can select the time most relevant to their needs, but the system pre-selects the current time


Expand|Select|Wrap|Line Numbers
  1. <%
  2. Dim MyTime    : MyTime     = FormatDateTime(now, 3) 'returns current date/time
  3. %>
  4.  
  5.     <tr>
  6.         <td class="body" align="right"><strong>Select Time In:</strong></td>
  7.         <td>
  8.             <select name="customer_arrivaltime">
  9.     <option selected="selected" value="<%=MyTime%>"><%=MyTime%></option>
  10.             <option value="05:30:00 AM">5:30 AM</option>             <option value="05:45:00 AM">5:45 AM</option>
  11.             <option value="06:00:00 AM">6:00 AM</option>            <option value="06:15:00 AM">6:15 AM</option>
  12.             <option value="06:30:00 AM">6:30 AM</option>            <option value="06:45:00 AM">6:45 AM</option>
  13.             <option value="07:00:00 AM">7:00 AM</option>            <option value="07:15:00 AM">7:15 AM</option>
  14.             <option value="07:30:00 AM">7:30 AM</option>            <option value="07:45:00 AM">7:45 AM</option>
  15.  
  16.         <option value="8:00:00 PM">8:00 PM</option>        
  17.             </select>        
  18.         </td>        
  19.     </tr>
This works great, except that I was an idiot and set it up to use the server time. The server is in the central time zone, now I have users on the east coast so the current pre-selected time is wrong. Bah.


So, I need to use the time on the users PC. It does not need to be atomically accurate, I don’t mind if the users PC has the wrong time. I have been playing with this javascript

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var newtime;
  3. var d = new Date();
  4. newtime = d.toLocaleTimeString();
  5. document.write(newtime);
  6. </script>
it produces the time in the format I want, but I can't figure out how to get the (newtime) results to appear in my dropdown so the user still sees their current time first.

can someone help? is there a better way to do this?


TIA
Path

GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#2: May 25 '09

re: use PC time in a dropdown list


Check out the JavaScript add() method for the Select Object.

http://www.javascriptkit.com/jsref/select.shtml
Newbie
 
Join Date: Jan 2009
Location: TX
Posts: 16
#3: May 29 '09

re: use PC time in a dropdown list


ok, so i red up on this option and have spent a lot of time fiddling with it to no avail. so far i have used this
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var newtime;
  3. var d = new Date();
  4. newtime = d.toLocaleTimeString();
  5.  
  6.  
  7. var myselect=document.getElementById("customer_arrivaltime")
  8. myselect.options[0]=new Option("Here Now", "who", "defaultSelected", "selected") //replace 1st option with a new one
  9.  
  10. </script> 
  11.  
to add an entry "Here now" to the top of my dropdown. but i cant figure out how to display the local PC time "newtime = d.toLocaleTimeString();" in the list.

please help before i go completely bald on this.
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#4: Jun 1 '09

re: use PC time in a dropdown list


Hi,

This works for me in IE and Chrome. Doesn't insert the option as the first one, but rather makes it selected:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type = "text/JavaScript">
  4. function addTimeOption()
  5. {
  6.  
  7. var myselect = document.myform.myselect;
  8. var thetime;
  9. var d = new Date();
  10. thetime = d.toLocaleTimeString();
  11.  
  12. var newOptn = document.createElement("OPTION");
  13. newOptn.text = "Here Now";
  14. newOptn.value = thetime;
  15. newOptn.selected = true;
  16. myselect.options.add(newOptn);
  17. }
  18. </script>
  19.  
  20. </head>
  21. <body onLoad="addTimeOption(); ">
  22. <form name = "myform">
  23. <select name="myselect">
  24. <option>Existing option</option>
  25. </select>
  26. <input type="submit" onClick="alert(document.all.myselect.options[document.all.myselect.selectedIndex].value)">
  27. </body>
  28. </html>
  29.  
Function is called on <body onLoad>

Hope that is of some use

Gaz.
Newbie
 
Join Date: Jan 2009
Location: TX
Posts: 16
#5: Jun 4 '09

re: use PC time in a dropdown list


Gaz, that worked fantastic, thank you very much for your time and help!
Reply