troubleshooting Question

C# Formatting DateTime String Value

Avatar of nightshadz
nightshadzFlag for United States of America asked on
XMLC#
5 Comments4 Solutions546 ViewsLast Modified:
I am formatting a date & time string value that is retrieved from an XML file and needs to be in this format: "yyyy-MM-dd HH:mm:ss:ff"

The string value I am working with is: "2011122818083869"

I have this working by creating 3 integer arrays with the positions for where I need to insert the dashes, spaces, and colons, but I'm curious if there is a more elegant way to code this in C#?

int[] dash = { 4, 7 };
int[] space = { 10 };
int[] colon = { 13, 16, 19 };
string s = attr.Value.Insert(dash[0], "-").Insert(dash[1], "-").Insert(space[0], " ").Insert(colon[0], ":").Insert(colon[1], ":").Insert(colon[2], ":");

The output returns "2011-12-28 18:08:38:69".

Thanks in advance!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 4 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros