views:

78

answers:

6

Hello, I am writing files to my harddrive,The filename is build like this:

String.Format("{0:yyyy-MM-dd_hh-mm-ss}.txt", DateTime.Now)

So the filename is "2010-09-20_09-47-04.txt" for example. Now I want to show those filenames in a dropdown, but with another format. The format should be dd.MM.yyyy HH:mm:ss. How can I do that, or is there a better approach?

Thanks :)

+2  A: 

You can parse it back into a DateTime, then format it to your display format.

Steven Sudit
+4  A: 

You can use DateTime.ParseExact with the same format you use to build name to parse the file name into DateTime object and than convert it to the desired format with ToString

Giorgi
That's a good idea. Alternately, `TryParseExact` might be a good idea, unless you really want to throw on failure here.
Steven Sudit
Parse/ParseExact dont seem to work with my format...I need the time as well.
grady
@grady - ParseExact should return time as well.
Giorgi
+1  A: 
string FileName = "2010-09-20_09-47-04";
DateTime dt = new DateTime();
dt = DateTime.Parse(FileName.Substring(0, 10));
dt.ToString("dd.MM.yyyy");
Barry
Thanks for the sample code, but Giorgi's suggestion about `ParseExact` is correct.
Steven Sudit
@Steven - Ah yes, TryParseExact slipped my mind. That would be a better option. Thanks
Barry
`ParseExact` or `TryParseExact` have the nice benefit of failing if the input is a valid date in an unexpected format. However, which of these two you want to use will depend on whether have an exception thrown is the best way to handle that failure.
Steven Sudit
A: 

Hi grady,

you can parse your filename using a regular expression:

Regex r = new Regex("(\d{4})-(\d{2})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2}).txt");
Match m = r.Match(fileName);

Update: The DateTime.Parse approach proposed by others is much more appropriate.

Since you are working with dates, you might want to consider using the created date or modified date of the file instead?

FileInfo f = new FileInfo(fileName);
string title = String.Format("{0:dd.MM.yyyy}", f.CreationTime);
chiccodoro
We need a Society for the Protection of Regular Expressions, to defend them from this kind of abuse. ;-)
T.J. Crowder
Well, I wouldn't consider this as abuse, if you suppose you have a string as input and need to parse it for a date. It's just that DateTime already provides it. Who knows whether they use Regex behind the scenes?
chiccodoro
They don't. But even if they did, that would not be a good reason for us to do it. :-)
Steven Sudit
A: 

I am sorry, I also need the time int the parsed string...

grady
Don't post comments as answers. Please either update your question, or post a comment under the question.
T.J. Crowder
Ok, I updated my original post
grady
You can also delete this answer in order to "clean up".
chiccodoro
+1  A: 

I tried this on console and it did the job.

Imports System.Globalization
Module Module1

Sub Main()
    Dim enUS As New CultureInfo("en-US")
    Dim d As String = Format(DateTime.Now, "yyyy-MM-dd_hh-mm-ss")
    Dim da As Date = DateTime.ParseExact(d, "yyyy-MM-dd_hh-mm-ss", enUS)
    Console.WriteLine("Date from filename: {0}", d)
    Console.WriteLine("Date formated as date: {0}", Format(da, "dd.MM.yyyy HH:mm:ss"))

End Sub
End Module

Hope it helps.

Besnik