在C#中将时间转换为格式化字符串

Mic*_*les 7 c# time string-formatting

Time.ToString("0.0")显示为十进制"1.5"代替1:30.如何让它以时间格式显示?

private void xTripSeventyMilesRadioButton_CheckedChanged(object sender, EventArgs e)
{
    //calculation for the estimated time label
    Time = Miles / SeventyMph; 
    this.xTripEstimateLabel.Visible = true;
    this.xTripEstimateLabel.Text = "Driving at this speed the estimated travel time in hours is: " + Time.ToString("0.0") + " hrs";
}
Run Code Online (Sandbox Code Playgroud)

Kel*_*sey 25

Time.ToString("hh:mm")
Run Code Online (Sandbox Code Playgroud)

格式:

HH:mm  =  01:22  
hh:mm tt  =  01:22 AM  
H:mm  =  1:22  
h:mm tt  =  1:22 AM  
HH:mm:ss  =  01:22:45  
Run Code Online (Sandbox Code Playgroud)

编辑:从现在起我们知道时间是double代码改变(假设你想要小时和分钟):

// This will handle over 24 hours
TimeSpan ts= System.TimeSpan.FromHours(Time);
string.Format("{0}:{1}", System.Math.Truncate(ts.TotalHours).ToString(), ts.Minutes.ToString());
Run Code Online (Sandbox Code Playgroud)

要么

// Keep in mind this could be bad if you go over 24 hours
DateTime.MinValue.AddHours(Time).ToString("H:mm");
Run Code Online (Sandbox Code Playgroud)