Time Part of DateTime Data Type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello ,

I have a sub that gets all records in a [Time] table. Then displays the
times in a drop-down list (ddlStartTime). My problem is that the ddl contain
the date AND time. i.e. 9:00 AM in the table is 1/1/1899 9:00:00 AM in the
ddl.

How do I get JUST the Time in format 9:00 AM??

Please I have been at this now for over 2 days and I can't seem to get
anywhere with it.

Thanks,
Mark
 
Hi,
Try with This One.............

MessageBox.Show(CType("1/1/1899 9:00:00 AM", DateTime).ToShortTimeString)

Regards,
Ritesh
 
Thanks Ritesh for the reply. However I am using web forms. I forgot to
mention that. Sorry.

Mark
 
Dim s as string
s=CType("1/1/1899 9:00:00 AM", DateTime).ToShortTimeString

s will be "9:00 AM"

What's the problem?
 
MGRIDEOUT said:
I have a sub that gets all records in a [Time] table. Then displays the
times in a drop-down list (ddlStartTime). My problem is that the ddl contain
the date AND time. i.e. 9:00 AM in the table is 1/1/1899 9:00:00 AM in the
ddl.

How do I get JUST the Time in format 9:00 AM??

Please I have been at this now for over 2 days and I can't seem to get
anywhere with it.

I'd suggest using theDateTime.ToString("hh:mm tt"), if you want to
definitely use that format rather than the locale-specific short time
format.
 
Greg,

I thought you where an American who had read that great book from Lederer
and Burdick (Americans).

\\\
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo("en-US")
Dim s As String = CType("1/1/1899 9:00:00 AM", DateTime).ToShortTimeString
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture
///

With a friendly smile on my face of course

(Did you see those samples I made about that displaymember by the way?, I
did it special because we where talking about it)

Cor
 
I was only reacting to the silliness of saying Ritesh's idea won't work with
a webform. Presumedly because of the msgbox call.

But it is true I should've been thinking more globally. :^)
(Did you see those samples I made about that displaymember by the way?, I
did it special because we where talking about it)

Which thread? Must've missed it.

Greg
 
Jon,
I'd suggest using theDateTime.ToString("hh:mm tt"), if you want to
definitely use that format rather than the locale-specific short time
format.
In my humble opinion does that not work in cultures where AM and PM is not
in the standard culture setting. I tried it before I sent my sample part,
because I thought as well that it would work forever but it did not. (Your
"hh:mm tt" is by the way the direct translation from what Greg has sent in
cultures where AM/PM is used.)

It is written on this page.
http://msdn.microsoft.com/library/d...ndatetimeformatinfoclassamdesignatortopic.asp

Cor
 
Cor Ligthert said:
In my humble opinion does that not work in cultures where AM and PM is not
in the standard culture setting.

You're right that it doesn't work in cultures where the AM designator
is not used at all. However, the designator may not be used in the
standard short time display, even if it's still present.
I tried it before I sent my sample part,
because I thought as well that it would work forever but it did not. (Your
"hh:mm tt" is by the way the direct translation from what Greg has sent in
cultures where AM/PM is used.)

Well, it may be - it may not be. For instance, some cultures may have
AM and PM *defined* (i.e. they *can* use the AM/PM designators) but
choose not to use them.

In other words, there are cultures where using ToShortTimeString
returns "20:05" where my code would return "8:05 PM". In fact, not only
are some cultures like that, but I happen to live in one :)

I've just run the following program:
using System;
class Test
{
static void Main()
{
DateTime dt = DateTime.Now;

Console.WriteLine (dt.ToString("hh:mm tt"));
Console.WriteLine (dt.ToShortTimeString());
}
}

and the results are:
10:19 PM
22:19
 
Back
Top