VB Net String to DateTime - vb.net

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)

Related

How can I only display the date from MySQL database?

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")

Difficulty with formatting a string to parse to date in vb.net

dim dt as string= "03/22/20 20:12:27.320"
Dim tempDate As DateTime = DateTime.ParseExact(dt, "MM/dd/yy hh:mm:ss.fff", CultureInfo.GetCultureInfo("en-US").DateTimeFormat)
This gives error sometimes : System.FormatException: 'String was not recognized as a valid DateTime.'
Why? It seems to be formatted properly. VB.net 4.6.1 framework
"hh" is 12-hour format. 20 is obviously not a valid hour in 12-hour format. If you want 24-hour format then use "HH".
Have you tried to use CDate() function?
dim dt as string= "03/22/20 20:12:27.320"
dim tempDate as Datetime = Cdate(dt)
or
dim tempDate as Datetime = Cdate("03/22/20 20:12:27.320")

VB.net yyyymmdd format needs to parse as date [duplicate]

I am developing asp.net site using vb framework 3.5.
Im having difficulties converting string data into Date
I tried using cdate function,
I have a variable sdate which is a string variable and date is stored in it which comes from textbox as dd/mm/yyyy now i want to convert this string into a Date variable as i need to perform the operations as Add a day or Subtract a day.
Please guide me how to go about this. i get the error on 3rd line as,String was not recognized as a valid DateTime. I have tried to do as follows but the error comes
Dim sdate As String
Dim expenddt As Date
expenddt = Date.Parse(edate)
expenddt = expenddt.AddDays(-1)
But i get the error as
Conversion from String to type Date is not valid.
How can I get a Date from the string?
You should have to use Date.ParseExact or Date.TryParseExact with correct format string.
Dim edate = "10/12/2009"
Dim expenddt As Date = Date.ParseExact(edate, "dd/MM/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo)
OR
Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date = Date.ParseExact(edate, format,
System.Globalization.DateTimeFormatInfo.InvariantInfo,
Globalization.DateTimeStyles.None)
OR
Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date
Date.TryParseExact(edate, format,
System.Globalization.DateTimeFormatInfo.InvariantInfo,
Globalization.DateTimeStyles.None, expenddt)
Nobody mentioned this, but in some cases the other method fails to recognize the datetime...
You can try this instead, which will convert the specified string representation of a date and time to an equivalent date and time value
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
MessageBox.Show(oDate.Day + " " + oDate.Month + " " + oDate.Year );
Try to see if the following code helps you:
Dim iDate As String = "05/05/2005"
Dim oDate As DateTime = Convert.ToDateTime(iDate)
Try converting date like this:
Dim expenddt as Date = Date.ParseExact(edate, "dd/mm/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
Hope this helps.
Try to use DateTime.ParseExact method, in which you can specify both of datetime mask and original parsed string.
You can read about it here: MSDN: DateTime.ParseExact

Convert a string to a datetime

I am developing asp.net site using vb framework 3.5.
Im having difficulties converting string data into Date
I tried using cdate function,
I have a variable sdate which is a string variable and date is stored in it which comes from textbox as dd/mm/yyyy now i want to convert this string into a Date variable as i need to perform the operations as Add a day or Subtract a day.
Please guide me how to go about this. i get the error on 3rd line as,String was not recognized as a valid DateTime. I have tried to do as follows but the error comes
Dim sdate As String
Dim expenddt As Date
expenddt = Date.Parse(edate)
expenddt = expenddt.AddDays(-1)
But i get the error as
Conversion from String to type Date is not valid.
How can I get a Date from the string?
You should have to use Date.ParseExact or Date.TryParseExact with correct format string.
Dim edate = "10/12/2009"
Dim expenddt As Date = Date.ParseExact(edate, "dd/MM/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo)
OR
Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date = Date.ParseExact(edate, format,
System.Globalization.DateTimeFormatInfo.InvariantInfo,
Globalization.DateTimeStyles.None)
OR
Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date
Date.TryParseExact(edate, format,
System.Globalization.DateTimeFormatInfo.InvariantInfo,
Globalization.DateTimeStyles.None, expenddt)
Nobody mentioned this, but in some cases the other method fails to recognize the datetime...
You can try this instead, which will convert the specified string representation of a date and time to an equivalent date and time value
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
MessageBox.Show(oDate.Day + " " + oDate.Month + " " + oDate.Year );
Try to see if the following code helps you:
Dim iDate As String = "05/05/2005"
Dim oDate As DateTime = Convert.ToDateTime(iDate)
Try converting date like this:
Dim expenddt as Date = Date.ParseExact(edate, "dd/mm/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
Hope this helps.
Try to use DateTime.ParseExact method, in which you can specify both of datetime mask and original parsed string.
You can read about it here: MSDN: DateTime.ParseExact

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.