Link to home
Start Free TrialLog in
Avatar of nightshadz
nightshadzFlag for United States of America

asked on

C# Formatting DateTime String Value

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], ":");

Open in new window


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

Thanks in advance!
SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nightshadz

ASKER

Thanks everyone!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial