Reputation: 97
I am trying to show time(s) in a dropdown. I managed to do so using this code:-
DateTime dtTime = new DateTime(2015,01,01,00,00,00);
DataTable dt = new DataTable();
dt.Columns.Add("time");
for (int i = 0; i < 48; i++)
{
string tm = dtTime.ToShortTimeString();
DataRow dr = dt.NewRow();
dr["time"] = tm;
dt.Rows.Add(dr);
dtTime = dtTime.AddMinutes(30);
}
ddlTime.DataSource = dt;
ddlTime.DataTextField = "time";
ddlTime.DataValueField = "time";
ddlTime.DataBind();
The output is :-
Now, I want to make the format hh:mm tt. for example, 1:00 AM should be 01:00 AM. Can anyone help me? Thanks.
Upvotes: 0
Views: 675
Reputation: 45947
replace
dtTime.ToShortTimeString();
with
dtTime.ToString("hh:mm tt");
Upvotes: 1