Solved

c# 2012 Format date from List

Posted on 2015-01-08
5
121 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 50

Accepted Solution

by:
Rgonzo1971 earned 500 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 85

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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