Solved

Drop Down List for Time and Date selection

Posted on 2004-10-13
8
690 Views
Last Modified: 2010-04-15
Hello, I need to create a drop down list to let a user select the start time and date of an event.  I would like the current date displayed and then the time.  For example the drop down list will have selections ranging grom 7am to 11pm, with the current date in front of the time.

Can anyone help me achieve this?  I'm not sure how to code it.

Thanks
0
Comment
Question by:dabrat
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:Realmrat
ID: 12305683
Is this a windows application or a ASP.NET application?

 - Joe
0
 
LVL 1

Author Comment

by:dabrat
ID: 12307215
asp.net application
0
 
LVL 3

Expert Comment

by:aacool
ID: 12310902
The code below uses System.Web.UI.WebControls.Calendar and a basic DropDownList - the dropdownlist can be made smarter as needed. Please tell me if this helps.

Try this:

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
   <script runat="server" >
 
      void Selection_Change(Object sender, EventArgs e)
      {

      // Get the selectedDate
      String selectedDate = Calendar1.SelectedDate.ToShortDateString();
      // Get the selected time
      string selectedTime = TimeList.SelectedItem.Value;
      //do processing with time and date
      // ...      
      }
   </script>
 
<body>
   <form runat="server">
      <h3> Date Time Picker </h3>
      Choose the date and time
      <br>
      <asp:Calendar id="Calendar1"
           ShowGridLines="True"
           ShowTitle="True"
           runat="server"/>
      <br>
      <table cellpadding="5">
         <tr>
            <td>
               Time:
            </td>
         </tr>
         <tr>
            <td>

               <asp:DropDownList id="TimeList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">
                  <asp:ListItem Selected="True" Value="7:00 AM"> 7:00 AM </asp:ListItem>
                  <asp:ListItem Value="8:00 AM"> 8:00 AM </asp:ListItem>
                  <asp:ListItem Value="9:00 AM"> 9:00 AM </asp:ListItem>
                  <asp:ListItem Value="10:00 AM"> 10:00 AM </asp:ListItem>
                  <asp:ListItem Value="11:00 AM"> 11:00 AM </asp:ListItem>
                  <asp:ListItem Value="12:00 AM"> 12:00 AM </asp:ListItem>
                  <asp:ListItem Value="1:00 PM"> 1:00 PM </asp:ListItem>
               </asp:DropDownList>

            </td>
        </tr>
     </form>
</body>
</html>
0
 
LVL 1

Author Comment

by:dabrat
ID: 12314240
this does not add the currnt date to the drop down list.  I need the drop down list to display current date then the time.

example ( each line would be a selection in the drop down list)

10/14/2004 7:00am
10/14/2004 7:30am

and so on
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 3

Expert Comment

by:aacool
ID: 12321652
Add this function to the Load event of the dropdown list:

private void TimeList_Load(object sender, System.EventArgs e)
{
     //TimeList is the drop down list
      TimeList.Items.Clear;
      //populate array of times - put in all relevant values
      string[] timeArrList = {" 7:00 AM"," 7:30 AM "," 8:00 AM "};
      string currDate = Calendar1.SelectedDate.ToShortDateString();
      for(int i=0;i<timeArrList.Length;i++)
      {
      timeArrList[i] = currDate + timeArrList[i];
      TimeList.Items.Add(timeArrList[i]
      }
}

This will show items in the drop down the way you want. Change the format of the date if you need to show it differently.
0
 
LVL 3

Accepted Solution

by:
aacool earned 50 total points
ID: 12321692
Small coding error  - missing bracket and ;

private void TimeList_Load(object sender, System.EventArgs e)
{
   TimeList.Items.Clear;
   string[] timeArrList = {" 7:00 AM"," 7:30 AM "," 8:00 AM "};
   string currDate = Calendar1.SelectedDate.ToShortDateString();
   for(int i=0;i<timeArrList.Length;i++)
  {
      timeArrList[i] = currDate + timeArrList[i];
      TimeList.Items.Add(timeArrList[i]);
   }
}
0
 
LVL 1

Author Comment

by:dabrat
ID: 12323362
okay the code you gave me works if I do not use my stored procedure but if I use it i get the following error:

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated
0
 
LVL 1

Author Comment

by:dabrat
ID: 12323997
thank you so much!!!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now