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!
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.


You could use regex
using System;
using System.Text.RegularExpressions;

public class Example
   public static void Main()
      string pattern =  @"(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})";
      string input = "2011122818083869";
      string replacement = "$1-$2-$3 $4:$5:$6:$7";
      Regex rgx = new Regex(pattern);
      string result = rgx.Replace(input, replacement);

      Console.WriteLine("Original String:    '{0}'", input);
      Console.WriteLine("Replacement String: '{0}'", result);                             

Open in new window

Gustav BrockCIOCommented:
You can just parse to true datetime, then format this using standard methods:

string input = "2011122818083869";

System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
DateTime date = DateTime.ParseExact(input, "yyyyMMddHHmmssff", provider);

string formattedDate = date.ToString("yyyy'-'MM'-'dd HH':'mm':'ss':'ff");

Console.WriteLine (formattedDate);

Open in new window

Fernando SotoRetiredCommented:
This should do what you need.
string dtStr = "2011122818083869";
var dtFormat = DateTime.ParseExact(dtStr, "yyyyMMddHHmmssFFFFFFF", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd HH:mm:ss:ff");

Open in new window

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
nightshadzAuthor Commented:
Thanks everyone!
Gustav BrockCIOCommented:
You are welcome!

Please note that the format string for ParseExact must be, well, exact. See my code.

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

From novice to tech pro — start learning today.