tags:

views:

60

answers:

7

i have a fileinfo array which loads jpegs from a folder.

When I add them to the list box using the creationtime property it adds 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?

A: 

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.

Brian R. Bondy
A: 

Use .ToString(format) method.

Values for format.

Adrian Godong
+2  A: 
sessionframeslstLISTBOX.Items.Add(fi.CreationTime.ToShortTimeString());
gbogumil
Though the ShortTime pattern will depend on the culture and may not be in 24 hour format.
ChrisF
thanks, oh i feel so dumb i had never used that method before :O
brux
A: 

Like this:

sessionframeslstLISTBOX.Items.Add(fi.CreationTime.ToString("t"));
SLaks
A: 
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());
}
Adkins
A: 

String.Format("{0:hh mm}", fi.CreationTime)

Alex
That'll put the day and the minute... are you sure you didnt mean {0:HH mm}
Jamiec
A: 

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

Adam Hughes