As popular dropdwonlist with datetime AM value and another dropdwonlist with PM C#

Asked

Viewed 52 times

0

Question: As popular a dropdownlist worthwhile datetime AM and other dropdownlist with PM value in C#.

private static void FillTimeDropDown(DropDownList dropDownList)
            {
                TimeSpan startTime = new TimeSpan(00, 00, 00); 
                TimeSpan endTime = new TimeSpan(23, 59, 00);
                DateTime StartTime = Convert.ToDateTime(startTime.ToString(@"hh\:mm", CultureInfo.CurrentCulture));// = new DateTime();

                DateTime EndTime = Convert.ToDateTime(endTime.ToString(@"hh\:mm", CultureInfo.CurrentCulture));

                int inc = 30;

                while (StartTime < EndTime)
                {
                    dropDownList.Items.Add(new ListItem { Text = StartTime.ToShortTimeString().ToString(CultureInfo.CurrentCulture), Value = StartTime.ToShortTimeString().ToString(CultureInfo.CurrentCulture) });
                    StartTime = StartTime.AddMinutes(inc);

                }
                dropDownList.Items.Add(new ListItem { Text = EndTime.ToShortTimeString().ToString(CultureInfo.CurrentCulture), Value = EndTime.ToShortTimeString().ToString(CultureInfo.CurrentCulture) });
  • Jesus, I suggest you write the statement of your question (I will edit to improve the formatting of the code), to make it as readable as possible.

1 answer

0


I made a code snippet with the idea to be used in your context:

//Variável que representa uma data e um horário qualquer
DateTime data = new DateTime(2010, 10, 10, 3, 00, 00);

//Variável para armazenar o formato do horário (AM ou PM) 
string formatoHorario = data.ToString("tt", CultureInfo.InvariantCulture);

//Variável que armazena o horário
string horario = data.ToString("hh:mm");

    // Se o formato do horário é AM, iremos pegar os dados e jogar no dropdown AM, caso contrário jogamos no dropdown PM
    if (formatoHorario == "AM") 
    {
        dropDownListAM.DataSource = listaHorariosAM;
        dropDownListAM.DataBind();
    }
    else 
    {
        dropDownListPM.DataSource = listaHorariosPM;
        dropDownListPM.DataBind();
    }

Remembering that it is just a functional example for you to adapt to your reality.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.