Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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#
Posted
Updated 4-Aug-16 23:12pm
Comments
Richard MacCutchan 4-Aug-16 9:40am    
Are you saying you do not know how to split a string, or use a format specification for time values? I would strongly suggest you start getting familiar with the documentation pages on MSDN.

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[^].
 
Share this answer
 
C#
//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"


So, to achieve your answer you need to convert output to DateTime and use the following formatting,
String.Format("{0:HH:mm}", output)


Following references will help you understanding more,
https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

String Format for DateTime [C#]
 
Share this answer
 
C#
string time = "19:15:00";
DateTime output = Convert.ToDateTime(time);
string desiredText = String.Format("{0:HH:mm}", output);
 
Share this answer
 
try
C#
string time = "19:15:00";
      time= time.Substring(0, time.LastIndexOf(':'));
 // or 
     time =  DateTime.ParseExact(time, "HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture).ToString("HH:mm");
 
Share this answer
 
Try:
C#
string s = value.Tables[0].Rows[0]["TripTime"].ToString();
this.TripTime.InnerText = s.Substring(0, s.Length - 2);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900