Wednesday, 25 June 2014

How to add Current Time to dropdown

System.DateTime dtCurrentDate = System.DateTime.Today;
System.DateTime dtCurrentTime = System.DateTime.Now;
ListItem listItem1 = new ListItem(dtCurrentTime.ToShortTimeString(), dtCurrentTime.ToShortTimeString());
//This will add Current time to dropdown.
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)
ddlTime.Items.Add(listItem1);
dtCurrentDate = dtCurrentDate.AddMinutes(30);
}