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.
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.