C# Format Date assigned to a string

I have a string which is storing a date value 10/30/2015 12:00:00 am.  I need to format it as 20151030.
LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
This should suffice:
using System;

namespace EE_Q28791680
{
	class Program
	{
		static void Main(string[] args)
		{
			DateTime current = new DateTime(2015, 10, 30);
			Console.WriteLine("The new string representation for {0:MM/dd/yyyy} is {0:yyyyMMdd}", current);
			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output -Capture.JPGYou can read more about Custom Date/Time Formats from here:

https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

-saige-
0
käµfm³d 👽Commented:
*No Points*

I like the presentation of formatters as laid out on http://blog.stevex.net/string-formatting-in-csharp/
0
Chris StanyonWebDevCommented:
You'll need to convert your string to a DateTime object, and then format that:

String originalDate = "10/30/2015 12:00:00 am";
String format = "MM/dd/yyyy hh:mm:ss tt";
DateTime newDate = DateTime.ParseExact(originalDate, format, CultureInfo.InvariantCulture);

Console.WriteLine(newDate.ToString("yyyyMMdd"));

Open in new window

1
CipherISAuthor Commented:
I tried the above recommendations.  This was the solution that worked for me.

var row = new ExportRow
{
....
DispDate = String.Format("{0:yyyyMMdd}", Convert.ToDateTime(result.dispDate))
....
};
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CipherISAuthor Commented:
Was the solution that resolved my problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.