Click here to Skip to main content
15,300,160 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i have a form which has timer tool on it to display the current date and time. the time is in the format 12/05/2014 12:17:20 but i want it to be in Fri 12 May 2014 12:17:20. please can someone tell me how to do that in c#.

If you convert your DateTime to a string, then you can use this format:
string formatted = dt.ToString("ddd dd MMM yyyy hh:mm:ss"); // change 'dt' into the name of your DateTime
First of all - DateTime stores it's value in binary and independent of visual representation (format), however it has a ToString() method, that can format the value as you wish:[^]
DamithSL 12-May-14 13:26pm
5d, wonder who is downvoting all answers, without any comment
Kornfeld Eliyahu Peter 12-May-14 13:31pm
Thank you...
(Don't be upset, last week I got a raw of 16 down-votes in less than 2 minutes! Part of them on already accepted answers!)
Thomas Daniels 12-May-14 13:33pm
Correct, a 5!
Kornfeld Eliyahu Peter 12-May-14 13:33pm
Thank you...
if you have datetime in string format like "12/05/2014 12:17:20" then better convert this to a DateTime type. DateTime can be convert to string format by giving the format which we needed.
for the String to DateTime conversion use DateTime.ParseExact[^] or DateTime.TryParseExact[^] methods
When you have the DateTime object, call the ToString method with custom format string as you need. Check Custom Date and Time Format Strings[^] for more information.

Sample code:

DateTime dt = DateTime.ParseExact("12/05/2014 12:17:20", "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
string result =dt.ToString("dd MMM yyyy hh:mm:ss");
Kornfeld Eliyahu Peter 12-May-14 13:18pm
OP talking about a timer, so obviously he has the time in DateTime...
DamithSL 12-May-14 13:25pm
Yes may be. But I don't want to put direct answer, added extra bit for OP and it will be helpful for future readers. Hope I haven't done wrong thing to give minimum vote :D
Kornfeld Eliyahu Peter 12-May-14 13:30pm
It's not me...I'm not the type...And you didn't do wrong at all I added my bit also for the future...
DamithSL 12-May-14 13:35pm
Thank you!
Kornfeld Eliyahu Peter 12-May-14 13:36pm
You are welcome! (See my answer it also got a 1 - it's life, at least in Q&A :-))
DamithSL 12-May-14 14:07pm
that's why I added "wonder who is downvoting all answers" when I'm vote you 5+
string dateStr = DateTime.Now.ToString("ddd dd MMM yyyy HH:mm:ss");

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