CipherIS
asked on
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.
*No Points*
I like the presentation of formatters as laid out on http://blog.stevex.net/string-formatting-in-csharp/
I like the presentation of formatters as laid out on http://blog.stevex.net/string-formatting-in-csharp/
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"));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Was the solution that resolved my problem.
Open in new window
Which produces the following output -You can read more about Custom Date/Time Formats from here:https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
-saige-