2

我有这个:

StringBuilder sb = new StringBuilder(time.Text);
if (DateTime.Parse(time.Text) > DateTime.Parse("12:00:00 AM")
    && DateTime.Parse(time.Text) < DateTime.Parse("11:59:59 AM"))                                    
{
    time.Text = time.Text + " AM";
}
else
{
    time.Text = time.Text + " PM";
}

我现在是下午 16:34,

我希望它显示 04:34 PM

4

4 回答 4

11

简单地

string strTime = DateTime.Now.ToString(@"hh\:mm\:ss tt");

在您的情况下,它将是:

 time.Text=DateTime.Parse(time.Text).ToString(@"hh\:mm\:ss tt");

并确保自定义格式,比如HH24 小时格式,MM是一个月的

于 2013-04-16T15:39:04.220 回答
4

尝试

time.Text = DateTime.Parse(time.Text).ToString("hh:mm:ss tt");
于 2013-04-16T15:40:23.893 回答
1
 public static string FormattedTime(this TimeSpan TimeIn24Hours)
    {
        String TimeIn12Hours = string.Empty;
        if (TimeIn24Hours != null)
        {
            TimeIn12Hours = DateTime.MinValue.AddHours(TimeIn24Hours.Hours).AddMinutes(TimeIn24Hours.Minutes).ToString("hh:mm");
        }
        return TimeIn12Hours;
    }
于 2014-02-27T10:47:07.070 回答
0
private void UpdateTime()
{
    int hours, mins, sec;
    string TimeofDate = "AM";
    currentTime = DateTime.Now;
    hours = Convert.ToInt32(currentTime.Hour.ToString());
    mins = Convert.ToInt32(currentTime.Minute.ToString());
    sec = Convert.ToInt32(currentTime.Second.ToString());

    // lbCurrentTime.Text = currentTime.ToLongTimeString();
    // label2.Text = hours.ToString() + ":" + mins.ToString() + ":" + sec.ToString();

    if (hours >= 12)
    {
        hours = hours - 12;
        TimeofDate = "PM";
    }
    else TimeofDate = "AM";

    lbCurrentTime.Text = hours.ToString() + ":" + mins.ToString() + ":" + sec.ToString()+" "+TimeofDate;
}
于 2015-08-16T00:57:21.137 回答