473,486 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2799
'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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2262
by: Gerry | last post by:
As I'm not a PHP-prgrammer at all, I just need Help with this: I have had a guestbook-page in Europe and will now have to move it to a US based-server. This makes the time-function showing time...
11
8088
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML....
2
6198
by: Todd_M | last post by:
I was wondering what anyone might suggest as "best practice" patterns for streaming out fixed formatted text files with C#? Let's say we get our data in a dataset table and we need to iterate over...
12
3127
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
3
3070
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
2
13250
by: elena | last post by:
Hi, All Please, i need help with cast string to DateTime: DateTime dt = new DateTime(2006,09,17); it works i can update Field in Access DB Next field is Short Time data type, how i can cast...
1
323
by: Cor Ligthert [MVP] | last post by:
Outlook time correction
1
2578
by: dhaneshrs | last post by:
I have a small code that shows inactive and active users from the ms access DB. <%@ Page Language="VB" MasterPageFile="~/MasterPageAdmin.master" Title="Welcome" %> <%@ Import...
1
1829
by: adeebraza | last post by:
Hi, Every Body Following is code for Showing Actual Date & Time on the form and also record Date & Time of an event. See the following and use Call Modified when you want to record an event in...
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.