Text cast to Date & Time

S

Stephany Young

'short date' and 'short time' are display formats, not variable types.

To deal with dates and/or times you first need to 'get' the source values
into a DateTime variables:

Dim _d As DateTime = DateTime.ParseExact(TextBox1.Text, "dd/MM/yyyy")

Dim _t As DateTime = DateTime.ParseExact(TextBox2.Text, "hh:mm tt"")

The time part of _d will represent midnight and the date part of _t will
represent 01/01/0001.

Note that, with the DateTime tyoe, midnight is ALWAYS considered to be the
start of the day.

If you need to combine the values, you can do it a number of ways. Two such
ways are:

Dim _dt As DateTime = DateTime.ParseExact(TextBox1.Text & " " &
TextBox2.Text, "dd/MM/yyyy hh:mm tt")

and:

Dim _d As DateTime = DateTime.ParseExact(TextBox1.Text, "dd/MM/yyyy")

Dim _t As DateTime = DateTime.ParseExact(TextBox2.Text, "hh:mm tt"")

Dim _dt As DateTime = _d.AddHours(_t.Hour).AddMinutes(_t.Minutes)

For display purposes:

ShortDate: Console.WriteLine(_dt.ToString("d"))

ShortTime: Console.WriteLine(_dt.ToString("t"))

ShortDate ShortTime: Console.WriteLine(_dt.ToString("g"))

Note that the ShortDate and ShortTime will honour your regional settings. If
you want it different to that then you need to use something like:

ShortDate: Console.WriteLine(_dt.ToString("dd/MM/yy"))

ShortTime: Console.WriteLine(_dt.ToString("hh:mm tt"))

ShortDate ShortTime: Console.WriteLine(_dt.ToString("dd/MM/yy hh:mm tt"))


Please note that the above is in the context of the original post to this
thread and is not meant to start a discussion of date/time formats in
different cultures.
 
C

Cor Ligthert [MVP]

Terry,

In my expirience is the CDate mostly the best to convert to Database values.

I did not try it in your time sample

Cor
 
T

Terry

Hi Stephany,

Thanks again for the help.

Had a warning about System.IFormatProvider on those first two lines. Tried
to find an answer but did not find anything detailed enough for me to
understand just now. Have you anything further detailed?

I also asked a few questions of the online help and came across the
following where I needed to populate the text boxes should they be left
empty. Is this the best method to achieve it?

Regards
Terry

dim thisDate as DateTime = DateTime.Now.
If (Me.txtBirthDate.Text = "") Then

Me.txtBirthDate.Text = thisDate.ToShortDateString

End If

If (Me.txtBirthTime.Text = "") Then

Me.txtBirthTime.Text = thisDate.ToShortTimeString

End If
 
T

Terry

Hi Stephany,

Found the answer as
Dim birthDateTime As DateTime = DateTime.Parse(birthDate & " " & birthTime)

It appears the format string was the problem, just relying on Parse to sort
it out worked just fine.

Regards



Terry
 
T

Terry

I have a TextBox with a date such as 15/01/2006 which I want to cast into a
variable as a short date 15/01/06, also
I need to cast a time such as 07:30 A.M. into a variable as a short time.
What is the best syntax for this please?

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top