Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Drop Down List for Time and Date selection

Posted on 2004-10-13
8
715 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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