如何在c#中将时间格式从h:m tt变为hh:mm tt

时间:2015-05-08 09:32:17

标签: c# time format

我想在下拉菜单中显示时间。我设法使用这段代码: -

        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();

输出结果为: -

enter image description here

现在,我想制作hh:mm tt格式。例如,凌晨1:00应该是01:00 AM。谁能帮我?感谢。

3 个答案:

答案 0 :(得分:1)

dtTime = dtTime.AddMinutes(30).ToString("HH:mm tt");

答案 1 :(得分:1)

替换

dtTime.ToShortTimeString();

dtTime.ToString("hh:mm tt");

答案 2 :(得分:1)

string tm = dtTime.ToString("hh:mm tt");