We help IT Professionals succeed at work.

C# Formatting DateTime String Value

nightshadz
nightshadz asked
on
519 Views
Last Modified: 2017-03-23
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!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Thanks everyone!
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION