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

0

Question: How popular is a dropdownlist with value datetime AM and another 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) });
    
asked by anonymous 31.07.2018 / 16:50

1 answer

0

I made a code snippet with the idea to be used in its 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 your reality.

    
31.07.2018 / 17:08