473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2792
'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
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
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
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
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
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
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
by: Cor Ligthert [MVP] | last post by:
Outlook time correction
1
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.