Search code examples
c#.netdatetimeiformatprovider

DateTime Format provider for filepath


I have a file-path that's been created from DateTime stamp:

"C:\\Logs\\Tests\\2015\\Mar\\24\\13_32_09\"

Now I am trying to convert my file-path back to DateTime object.

With Regex I can easily remove "C:\\Logs\\Tests\", but now I am assume I need to provide implementation of IFormtProvider to convert 2015\\Mar\\24\\13_32_09\ into a DateTime object, but I haven't come along any similar example of how that's usually done.

Any example, may not be particular solution to my answer, would be helpful.

Thanks


Solution

  • You can use DateTime.ParseExact like:

    DateTime dt = DateTime.ParseExact("2015\\Mar\\24\\13_32_09\\", 
                                      @"yyyy\\MMM\\dd\\HH_mm_ss\\", 
                                      CultureInfo.InvariantCulture);