Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c# 2012 Format date from List

Posted on 2015-01-08
5
Medium Priority
?
132 Views
Last Modified: 2015-01-09
How do I format the date in this scenario?  The format string doesn't work and I've tried others.

string sDate = list[x].StartDate.ToString();
sDate = String.Format("{0:ddd, MMM d, yyyy}", sDate);

Thanks!
0
Comment
Question by:Sheryl Landis
5 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40539540
How do you display it? In what control; perhaps via a message box?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 40539618
You can try something like this.

DateTime thisDate1 = new DateTime(2011, 6, 10);
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".");
for exact answer please share what you are getting in string sDate
which will help us to understand what is required
0
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 1500 total points
ID: 40539652
HI,

If StartDate a DateTime variable then try

sDate = String.Format("{0:ddd, MMM d, yyyy}", list[x].StartDate);

else try

string sDate = list[x].StartDate.ToString();
sDate = String.Format("{0:ddd, MMM d, yyyy}", Convert.ToDateTime(sDate));

Regards
0
 

Author Closing Comment

by:Sheryl Landis
ID: 40540041
Here is what worked, and your solution prompted me to try it this way:

string sDate = String.Format("{0:ddd, MMM d, yyyy}", list[x].StartDate);

Before, I was converting StartDate .ToString() first, which didn't work:

string sDate = String.Format("{0:ddd, MMM d, yyyy}", list[x].StartDate.ToString());

Thank you all for your input!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40540575
Just use ToString() directly on the StartDate as alluded to by Pratima Pharande in the previous post back here.

With your format it would look like this:
string sDate = list[x].StartDate.ToString("ddd, MMM d, yyyy");

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question