How can I only display the date from MySQL database? - vb.net

I only want to display the date from my database to a textbox which is a "yyyy/MM/dd" format. The problem is when I'm retrieving the data, it comes out as "28/01/2022 12:00:00 AM". How can I display just the date?

If you have a datetime field in SQL. You read into a vb.net variable
Dim myDateTime As DateTime = getDateTimeValueFromSQL()
or if you have a DateTime stored as a string in SQL (this is bad practice) you parse the string into a DateTime
Dim myDateTimeString As String = getDateTimeStringFromSQL()
Dim myDateTime As DateTime = DateTime.ParseExact(myDateTimeString, "dd/MM/yyyy hh:mm:ss tt", Globalization.CultureInfo.InvariantCulture)
' "dd/MM/YYYY hh:mm:ss tt" here according to format in SQL
then if you want to change how it is displayed as a string, you just use a string format method
TextBox1.Text = $"{myDateTime:yyyy/MM/dd}"
' or
TextBox1.Text = myDateTime.ToString("yyyy/MM/dd")

Related

VB Net String to DateTime

from string dataIni = "2015/12/21 18:28", I would like to parse it into same format DateTime. I am using following code,
Dim dataIni = "2015/12/21 18:28"
Dim dataIniParsed As DateTime = DateTime.ParseExact(dataIni, "yyyy/MM/dd HH:mm", System.Globalization.CultureInfo.InvariantCulture)
However, I am getting "21/12/2015 6:28:00 PM". How to get desired format?
Once you have parsed the text into a DateTime you no longer have a "format" as a DateTime is a data type that just represents a time and a date without any particular format. It's not until you do a .ToString() or Console.WriteLine(...), for example, that the DateTime is converted to a string.
Now, by default, .ToString() produces a string format based on your system settings. Yours is clearly producing "21/12/2015 6:28:00 PM". On my system I use an ISO date format, so my .ToString() produces "2015/12/21 18:28:00" for your date.
You can produce custom formats easily though. Try this code:
Dim dataIniFormatted As String = dataIniParsed.ToString("yyyy/MM/dd HH:mm")
This produces the string "2015/12/21 18:28". Note that this is no longer a DateTime - it's a string - and also that dataIniParsed is still a DateTime (with no inherent formatting).
You can use the code below to format your Date variable. You have to format the date to get the desired result.
Dim dataIni As String = "2015/12/21 18:28"
Dim dataIniParsed As DateTime
dataIniParsed = Convert.ToDateTime(dataIni)
MsgBox(dataIniParsed & " - " & dataIniParsed.ToString("yyyy/MM/dd HH:mm") & " - " & dataIniParsed.GetType.ToString)

How CDATE perform its conversion from string to date on VB.NET

I just want to ask how VB.NET perform is conversion of date from string. I am currently designing a database view and convert the date into MMddyyyy format, but I am worried that CDATE it will read it as ddMMyyyy.
I want it to make it shorter and converting the date using CDATE instead of using the traditional M/d/yyyy h:mm:ss tt.
Example:
Dim myDate As Date = CDate(DataTable.Rows(0).Item("DateValue").ToString())
CDate depends on the control panel regional settings and is not recommended - you should use Date.ParseExact instead
Const MyDateFormat As String = "MMddyyyy"
Dim dte As Date = #2/1/2003#
'convert the date to a string
Dim strDate As String = dte.ToString(MyDateFormat)
'convert the string back to a date
Dim dte2 As Date = Date.ParseExact(strDate, MyDateFormat, System.Globalization.CultureInfo.InvariantCulture)
If dte = dte2 Then
MsgBox("They're the same :-) " & strDate)
Else
MsgBox("They're different :-(")
End If
For your code, it would look like:
Dim myDate As Date = Date.ParseExact(DataTable.Rows(0).Item("DateValue").ToString(), "MMddyyyy", System.Globalization.CultureInfo.InvariantCulture)

VB.NET String Data Manipulation for DateTimePicker

I want to manipulate my String data for example I have a x="20140118"
to y="01/18/2014" how can I do it? I need it for the value in DateTimePicker on VB.NET.
Thanks
DateTimePicker.Value wants a DateTime not a string. So you need to parse it:
Dim dt As DateTime = DateTime.ParseExact("20140118", "yyyyMMdd", CultureInfo.InvariantCulture)
dateTimePicker1.Value = dt
DateTime.ParseExact
Custom Date and Time Format Strings
However, just for the sake of completeness, if you need a string 01/18/2014 from the DateTime you can use DateTime.ToString:
Dim date As String = dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
If that is your local date format you could also use these more concise approaches:
)
Dim date As String = dt.ToShortDateString()
)
Dim date As String = dt.ToString("d")

Convert String to Date Time [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert string to DateTime in c#
I am trying to convert a string in the format "20110617111051" to date time.
Currently I am using String.SubString() function to extract year, month, day, time to format a standard string and then using Convert.ToDateTime(string). Is there any other simple way to do this?
Dim x as String="20110617110715"
Dim standard as string = x.SubString(0,4) & "-" & x.SubString(4,2) & "-" & x.SubString(6,2) 'and time
Dim dateTime as DateTime = Convert.ToDateTime(standard)
You can use DateTime.ParseExact.
DateTime date = DateTime.ParseExact(x, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
VB
Dim myDate as DateTime = DateTime.ParseExact(x, "yyyyMMddHHmmss", CultureInfo.CurrentCulture)
Use the DateTime.ParseExact function in conjunction with the exact format of your input string. Example:
C#:
string input = "20110617111051";
string format = "yyyyMMddhhmmss";
DateTime dateTime = DateTime.ParseExact(input, format, CultureInfo.InvariantCulture);
VB:
Dim input As String = "20110617111051"
Dim format As String = "yyyyMMddhhmmss"
Dim dateTime as DateTime = DateTime.ParseExact(input, format, CultureInfo.CurrentCulture)
See this page for more info on custom date and time strings.

VB.net ISo DateFormat

I have a date in VB.net that is stored in ISO 8601 format
'Date here is local time in germany
Dim s As String = "2010-09-27T16:54:28+02:00"
Dim dt As DateTime
If Date.TryParse(s, dt) = True Then
End If
When I use try to parse it shows me date in my local timezone,
how can I get date as
2010-9-27 4:54 PM as well as GMT DATE ?
A date is not stored internally with any specific representation.
You need to format it for display, using the correct DateTime format string (either custom or standard):
Dim s As String = "2010-09-27T16:54:28+02:00"
Dim dt As DateTime
If Date.TryParse(s, dt) = True Then
Dim gmt as String = dt.ToUniversalTime().ToString("r", CultureInfo.InvariantCulture)
Dim custom as String = dt.ToUniversalTime().ToString("yyyy-M-d h:mm tt", CultureInfo.InvariantCulture)
End If