Convert date and time

I have an object "2/17/2011 6:46:01 PM". I want to convert this object to 6:46 pm

+4
source share
5 answers

You can format the object as

strdate = convert.todatetime(object); strdate .tostring("hh:mm tt"); 

or

 strdate.toshorttime(); 
+4
source
 string myDateString = "2/17/2011 6:46:01 PM"; DateTime datetime = DateTime.Parse(myDateString); string timeString = datetime.ToShortTimeString(); Console.WriteLine(timeString); // 6:46 PM 

You can format the parsed datetime into a string in many other ways, but ToShortTimeString does exactly what you want.

+11
source

Maybe you only need a format ?!

 DateTime.Parse(obj.ToString()).ToString("h:mm tt"); 
+3
source
 using System.Globalization; 

...

 string dateString, format; format = "M/dd/yyyy h:mm:ss tt"; dateString = "2/17/2011 6:46:01 PM"; DateTime result; CultureInfo provider = CultureInfo.InvariantCulture; result = DateTime.ParseExact(dateString, format, provider); Console.WriteLine(result.ToString()); 

See here for more information: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

+2
source

If the input is a string, first convert it to a dateTime method using the DateTime.parse method, and then collapse it to shortTimeString or another. If input, DateTime converts it to shorttimeString in this form: input.toShortTimeString

+1
source

Source: https://habr.com/ru/post/1340139/


All Articles