如何分割时间 [英] How to split the time
问题描述
this.TripTime.InnerText = value.Tables [0] .Rows [0] [TripTime]。ToString();
我的输出如下
19:15:00
从上面的输出我想要如下
19:15
我怎样才能在asp.net中使用c#
我尝试了什么:
如何分配时间asp.net使用C#
this.TripTime.InnerText = value.Tables[0].Rows[0]["TripTime"].ToString();
My output as follows
19:15:00
From the above output i want as follows
19:15
for that how can i do in asp.net using c#
What I have tried:
how to split the time in asp.net using C#
推荐答案
作为 Richard MacCutchan [ ^ ]提到,你必须阅读MSDN文档:
标准日期和时间格式字符串 [ ^ ]
自定义日期和时间格式字符串 [ ^ ]
另一方面,你可以覆盖ToString()方法 [ ^ ]。
As Richard MacCutchan[^] mentioned, you have to read MSDN documentation:
Standard Date and Time Format Strings[^]
Custom Date and Time Format Strings[^]
On the other side, you can override ToString() method[^].
//Consider you already have the output ins tring
string strOutput = "19:15:00";
//Convert it to DateTime
DateTime output = Convert.ToDateTime(strOutput);
//Now you can manipulate and format the DateTime
Console.WriteLine(String.Format("{0:HH:mm}", output)); // 19:15
Console.WriteLine(String.Format("{0:hh:mm}", output)); // 07:15
//You can do the folowings as well
Console.WriteLine(output.TimeOfDay); // {19:15:00}
Console.WriteLine(output.ToShortTimeString()); // "7:15 PM"
Console.WriteLine(output.ToLongTimeString()); // "7:15:00 PM"
因此,要获得答案,您需要将输出转换为DateTime并使用以下格式,
So, to achieve your answer you need to convert output to DateTime and use the following formatting,
String.Format("{0:HH:mm}", output)
以下参考资料将帮助您了解更多信息,
< a href =https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396> https://msdn.microsoft。 com / zh-CN / library / az4se3k1%28v = vs.110%29.aspx?f = 255& MSPPError = -2147217396
DateTime的字符串格式[C#]
string time = "19:15:00";
DateTime output = Convert.ToDateTime(time);
string desiredText = String.Format("{0:HH:mm}", output);
这篇关于如何分割时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!