469,616 Members | 1,823 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,616 developers. It's quick & easy.

Text cast to Date & Time

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
Jan 18 '07 #1
4 2543
'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.
"Terry" <ne*******@whiteHYPHENlightDOTme.ukwrote in message
news:uL**************@TK2MSFTNGP06.phx.gbl...
>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

Jan 18 '07 #2
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

"Terry" <ne*******@whiteHYPHENlightDOTme.ukschreef in bericht
news:uL**************@TK2MSFTNGP06.phx.gbl...
>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

Jan 18 '07 #3
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

"Stephany Young" <noone@localhostwrote in message
news:ud*************@TK2MSFTNGP06.phx.gbl...
'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.
"Terry" <ne*******@whiteHYPHENlightDOTme.ukwrote in message
news:uL**************@TK2MSFTNGP06.phx.gbl...
>>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


Jan 18 '07 #4
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

"Stephany Young" <noone@localhostwrote in message
news:ud*************@TK2MSFTNGP06.phx.gbl...
'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.
"Terry" <ne*******@whiteHYPHENlightDOTme.ukwrote in message
news:uL**************@TK2MSFTNGP06.phx.gbl...
>>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


Jan 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gerry | last post: by
12 posts views Thread by Frederik Vanderhaeghe | last post: by
1 post views Thread by Cor Ligthert [MVP] | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.