Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi in my below code i want to remove the Seconds from the date.Even I gave string format with out seconds but the DateTime.ParseExact is giving the time with the seconds(25:50:00)"00".How can i resolve this issue so that my time string comes with out any seconds


dateString = "08/14/2013 12:25 PM"
format = "MM/dd/yyyy hh:mm tt"

result = DateTime.ParseExact(dateString, format, provider);
string alteredDt = result.ToString(CultureInfo.CurrentCulture.DateTimeFormat);
Console.WriteLine(alteredDt}
Posted

DateTime is stored internally as ticks. When you create a datetime, there are always seconds (even milliseconds) that are stored in the structure. You can't change this. Using different methods on the DateTime, like .Today give the date part, but the other parts of it are zero. This is by design.

If you don't want to display the seconds when you create the string, use a format string, like MM/dd/yyyy hh.mm and leave the tt part off.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Aug-13 10:34am    
All correct, but I'm not sure this is where OP is confused. Please see my answer.
—SA
How come you know how to use the string format for System.DateTime for parsing and fail to do so for ToString?

Please see: http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^].

In this method, the parameter is the format. Please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Use custom format string and include hours and minutes, but not seconds ('s', 'ss'), pretty much as it's already done in your format string "format".

—SA
 
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