file.creation formatting in c#
i have a fileinfo array which loads jpegs from a folder.
When I add them to the list box using the creationtime property it a开发者_如何学Godds both the date and time, for example: "07/07/2010 14:28"
Here is the code I use:
foreach (FileInfo fi in files5)
{
sessionframeslstLISTBOX.Items.Add(fi.CreationTime);
}
I want it so that It just says the time, ege: "14:28"
How can I format the string before its added to the listbox so that it omits the date?
sessionframeslstLISTBOX.Items.Add(fi.CreationTime.ToShortTimeString());
Use the format specifier: "{0:t}"
from String.Format, or "t"
from your DateTime
object's .ToString()
or you can also use .ToShortTimeString()
from your DateTime
object.
Use .ToString(format)
method.
Values for format.
Like this:
sessionframeslstLISTBOX.Items.Add(fi.CreationTime.ToString("t"));
fi.CreationTime.ToShortTimeString()
That should do the trick for you. To show it in your own code it would look like this:
foreach (FileInfo fi in files5)
{
sessionframeslstLISTBOX.Items.Add(fi.CreationTime.ToShortTimeString());
}
You can format the CreationTime by calling ToString on it and passing it a format string.
It looks like you probably want fi.CreationTime.ToString("HH:mm")
, but look at the other format strings available:
Standard Time and Date Format Strings
Custom Time and Date Format Strings
String.Format("{0:hh mm}", fi.CreationTime)
精彩评论