Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a datetime as filename and parse the filename afterwards?

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 :)

like image 663
grady Avatar asked Dec 05 '25 15:12

grady


2 Answers

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.

like image 52
Besnik Avatar answered Dec 07 '25 04:12

Besnik


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

like image 41
Giorgi Avatar answered Dec 07 '25 05:12

Giorgi