Solved

c# 2012 Format date from List

Posted on 2015-01-08
5
111 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 33

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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now