Click here to Skip to main content
15,793,547 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi all,

Does .NET has API for converting DateTime to mm/dd/yyyy hh:mm:ss?
What i need is:
08/09/2011 04:05:02

8/9/2011 4:05:02

I've tried splitting and concatenating strings to get my desired output but it's code is too long. I need a short code either in C# or T-SQL. And also, the format of time would be 24hrs

i've been searching google for 2 hrs but still could not come up with a solution. Any answer would be appreciated.

Thanks :)
Updated 6-Dec-11 22:55pm
Sergey Alexandrovich Kryukov 7-Dec-11 3:38am    
I'm amazed: do you and other people do it on purpose?! "What I need is: 08/09/2011..." says nothing about what you want because both 8 and 9 can be either month or day! Do you intentionally choose the ambiguous examples?!!
If you said 13/01/2011 vs. 01/13/2011 it would be clear. Wow!
[no name] 7-Dec-11 3:55am    
wrong question btw. editing :)

what i mean is 8/9/2011 to 08/09/2011

This is not a conversion. It's important to understand: you need to work with time data, not its string representation until you need to present data in UI, for example. What you need is formatting of data.

You need to use System.DateTime.ToString with some parameters defining formatting rules which you can find in the MSDN help pages referenced below. See:[^] (see all the methods ToString),[^] (standard format strings),[^] (custom format strings).

Share this answer
Kim Togo 7-Dec-11 3:17am    
Total correct SA my 5.
Only convert DateTime to string when needed to show in UI controls.
And when it has to be showed in UI, then please please follow the Regional settings.
Sergey Alexandrovich Kryukov 7-Dec-11 3:31am    
Regional settings is a good point. I think the best way is working in UTC with all application data, converting it to local time only before doing ToString as explained above.
Thank you, Kim.
[no name] 7-Dec-11 3:26am    
still didnt fix my problem :( i want 08:03:02 for ex, instead of 8:03:02.. anyway thanks for this SA! i've used CONVERT(varchar(19), GETDATE(), 120) instead :)
Sergey Alexandrovich Kryukov 7-Dec-11 3:32am    
By you still can fix it... :-)
The most simple way is to use DateTime.ToString("s")[^] it will give an output that is "yyyy'-'MM'-'dd'T'HH':'mm':'ss". - 2008-04-10T06:30:00

The pattern reflects a defined standard (ISO 8601), and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied.
Share this answer

DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm:ss"));

This may help you,

Share this answer
I'd try the TryParse method with the appropriate formatting[^].
Share this answer
Look at DateTime.ToString[^]

There is a complete list of the formats here: Formatting a DateTime for display - format string description[^]
Share this answer
Here is another solution , if you run this :

DateTime dt = new DateTime(2011, 9, 11, 23, 5, 2);
var dtString = dt.ToString(@"MM\/dd\/yyyy HH:mm:ss");

You will get :
"09/11/2011 23:05:02"

in dtString.

Hope it helps.
Share this answer
Amir Mahfoozi 7-Dec-11 5:25am    
Thanks Richard for your kind attention but the "\/" is to force to show "/" character instead of "-" character :)
Try this :

MM/dd/yyyy hh:mm:ss tt
Share this answer
hi Eduard Lu,

check this link;[^]

Hope it helps!
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