Solved

c# 2012 Format date from List

Posted on 2015-01-08
5
107 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 48

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

16 Experts available now in Live!

Get 1:1 Help Now