SkyeBoniwell
SkyeBoniwell

Reputation: 7092

Format this datetime without seconds or AM/PM

I have a property that is set to "DateTime?" like this:

Public Overridable Property MyDateTime As DateTime?

I'm trying to get the hours and minutes without the seconds and without AM or PM.

I tried this:

MyDateTime.Value.TimeOfDay.ToString

That gives me the hours, minutes, and seconds without AM or PM

And I tried this:

MyDateTime.Value.ToShortTimeString

This gives me only the hours and minutes, but also gives me AM or PM.

Is there a way to format it so it looks like this?

9:10

or

2:40

Thanks

Upvotes: 2

Views: 5518

Answers (4)

senthilkumar2185
senthilkumar2185

Reputation: 2566

MyDateTime.Value.ToString("hh:mm tt")

This Code show time with AM or PM

Upvotes: 1

Mike Christensen
Mike Christensen

Reputation: 91600

Try:

MyDateTime.Value.ToString("h:mm") ' 12 Hour Clock

Or:

MyDateTime.Value.ToString("H:mm") ' 24 Hour Clock

Upvotes: 3

Khan
Khan

Reputation: 18142

The ToString method accepts formatting parameters.

E.g. (12 hour clock)

MyDateTime.Value.ToString("h:mm")

(24 hour clock)

MyDateTime.Value.ToString("H:mm")

Upvotes: 3

vcsjones
vcsjones

Reputation: 141598

You are looking for a custom Date and Time format.

Try this:

MyDateTime.Value.ToString("HH:mm")

Upvotes: 2

Related Questions