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

发布于 2024-08-30 21:30:19 字数 444 浏览 4 评论 0原文

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";
}

Time.ToString("0.0") shows up as a decimal "1.5" for instead of 1:30. How can I get it to display in a time format?

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";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

倾城°AllureLove 2024-09-06 21:30:20
Time.ToString("hh:mm")

格式:

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  

编辑:从现在起我们知道时间是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());

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

Formats:

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  

EDIT: Since now we know the time is a double change the code to (assuming you want hours and minutes):

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

or

// Keep in mind this could be bad if you go over 24 hours
DateTime.MinValue.AddHours(Time).ToString("H:mm");
熟人话多 2024-09-06 21:30:20

如果 TimeSystem.Double,则 System.TimeSpan.FromHours(Time).ToString();

If Time is a System.Double, then System.TimeSpan.FromHours(Time).ToString();

对你而言 2024-09-06 21:30:20

我猜 Time 的类型是 TimeSpan?在这种情况下,TimeSpan.ToString 的文档可以帮助您,特别是页面

如果时间是数字数据类型,则可以使用 TimeSpan.FromHours 首先将其转换为 TimeSpan。

(编辑:TimeSpan 格式字符串是在 .NET 4 中引入的。)

I guess that Time is of type TimeSpan? In that case, the documentation of TimeSpan.ToString can help you, in particular the pages

If Time is a numeric data type, you can use TimeSpan.FromHours to convert it to a TimeSpan first.

(EDIT: TimeSpan format strings were introduced in .NET 4.)

甲如呢乙后呢 2024-09-06 21:30:20

请注意,如果您在 24 小时制中工作,则使用 HH:mm 而不是 hh:mm 非常重要。

有时我错误地写了 hh:mm,然后我得到的不是“13:45”而是“01:45”,并且无法知道现在是上午还是下午(除非您使用 tt)。

Note that if you work in a 24-hour base, it's very important to use HH:mm and NOT hh:mm.

Sometimes I mistakenly write hh:mm, and then instead of "13:45" I get "01:45", and there's no way to know whether it's AM or PM (unless you use tt).

有木有妳兜一样 2024-09-06 21:30:20

如果时间是浮动或双倍,你就必须这样做。
System.Math.Truncate(Time) 获取小时数

,然后 (Time - System.Math.Truncate(Time))* 60
获取会议记录。

If time is float or double you'll have to.
System.Math.Truncate(Time) to get the hours

and then (Time - System.Math.Truncate(Time))* 60
to get the minutes.

三生池水覆流年 2024-09-06 21:30:20

感谢所有的回复,我在我的程序中使用了这个 DateTime.MinValue.AddHours(Time).ToString("H:mm"); ,因为它是最容易实现的一个。

Thanks for all of the responses guys and gals i used this DateTime.MinValue.AddHours(Time).ToString("H:mm");for my program since it was the easiest one to implement.

欲拥i 2024-09-06 21:30:20

从数值变量创建时间跨度:

TimeSpan ts = new TimeSpan(Math.Floor(Time), (Time - Math.Floor(Time))*60);

然后,使用 ToString 方法。

Create a timespan from you numeric variable:

TimeSpan ts = new TimeSpan(Math.Floor(Time), (Time - Math.Floor(Time))*60);

Then, use the ToString method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文