ASP.Net: Display only time in dropdown list
This resource will show the time only in ASP.Net dropdown list. This code snippet will help to display the 24 hr time with 30 minutes interval duration in the dropdown list. This is the customizable code so that if need to display it in 15 minutes duration, we just need to change it from 30 to 15. Or if we want to display each and every minutes as the interval then we just need to change it to 1 in place of 30, it will display the time accordingly. Find ASP.Net: Display only time in dropdown list.
Learn something about ASP.Net: Display only time in dropdown list
Hai Friends,
Sometimes we think that why the ASP.Net controls are not according to our requirements? Why they have that much less functionality?
Why Microsoft has not provided such time of control which I can implement in our project to finish our task??
How many of us face these types of problems in our day to day programming language?
In this regards, today I thought lets better to do something with Dropdown list…
As in ASP.Net, how to show only the time in dropdown list is our today's topic and let's go with it.
As we know that there is no Built in control which can show the time in Asp.Net (We have datetime picker in windows application where we can choose the enum value as time only to show the time).
So we will take help of Dropdown list to show the time only to fulfill our one of the customer's requirements.
So let's first start with adding a dropdown list to our small application.
Take a .aspx page as I took WebForm1.aspx and drag a Dropdown list with the name “ddlTime".
Now we need to write the code to display the time in our dropdown list as :
using System;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime dtCurrentDate = DateTime.Today;
DateTime dtCurrentTime = DateTime.Now;
ListItem listItem1 = new ListItem(dtCurrentTime.ToShortTimeString(), dtCurrentTime.ToShortTimeString());
listItem1.Enabled = listItem1.Selected = true;
for (int i = 0; i <= 48; i++)
{
ListItem listItem2 = new ListItem(dtCurrentDate.ToShortTimeString(), dtCurrentDate.ToShortTimeString());
listItem2.Selected = false;
ddlTime.Items.Add(listItem2);
if (dtCurrentDate.CompareTo(dtCurrentTime) < 0 && dtCurrentDate.AddMinutes(30).CompareTo(dtCurrentTime) > 0)
ddlTime.Items.Add(listItem1);
dtCurrentDate = dtCurrentDate.AddMinutes(30);
}
}
}
}
Now let's understand what I have done here.
Step #1: Getting the current date by using:
DateTime dtCurrentDate = DateTime.Today;
Step #2: Getting the current time as:
DateTime dtCurrentTime = DateTime.Now;
Step#3: Taking a list item and adding the time with its value.
Step#4: A for loop which will add the 0 to 48 hours in the dropdown list. Inside the for loop, we are comparing the current time and the time which is already in the list2 so that based on the comparison, we are showing the current time displayed in the dropdown list.
Hope it will help to all of us to display the time in dropdown list.
To display only time in dropdownlist we can give like this also
dropdownlist.items.add(datetime.now.tostring("hh:mm:ss"));