I have the following code which worked fine on a .net 1.1 website:
objSQLCommand.Parameters.Add(New SqlParameter("#date", SqlDbType.DateTime, 8))
objSQLCommand.Parameters("#date").Value = txtDate.Text
This doesn't work on a .net 3.5 server.
I'm getting a message saying that it can't convert a string to datetime.
Try this one,
objSQLCommand.Parameters("#date").Value = Convert.ToDate(txtDate.Text)
follow-up question, what is the format of the date in your textbox? Maybe you can take advantage of TryParse or ParseExact also.
Dim provider As CultureInfo = CultureInfo.InvariantCulture
Dim dateString as String = txtDate.Text '08/10/2012
Dim format As String = "d"
objSQLCommand.Parameters("#date").Value = Date.ParseExact(dateString, format, provider)
Related
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")
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
I'm trying to use this code:
If Not IsError(Convert.ToDateTime(DatiBase(i).innertext.substring(DatiBase(i).innertext.indexof(":") + 1).trim(), New CultureInfo("it-IT"))) Then
MyString = "'" & Convert.ToDateTime(DatiBase(i).innertext.substring(DatiBase(i).innertext.indexof(":") + 1).trim(), New CultureInfo("it-IT")).ToString("yyyy-MM-dd") & "'"
Else
'Do something else
End If
My goal is to assign value to "MyString" only if "Convert.ToDateTime" gives a valid value but the code doesn't do that: It stops when the String isn't recognized as a valid DateTime instead of execute the "else" code.
You should use DateTime.TryParse:
https://msdn.microsoft.com/en-us/library/9h21f14e%28v=vs.110%29.aspx
If you don't know exactly what is the format in which you receive your string then you need to provide a set of possible formats to the DateTime.TryParseExact.
This example below is a simplified test version of your code above
Sub Main
Dim dateConverted as DateTime
Dim testDate = "asdfasldka:08/05/2015 13:10"
Dim yourWannaBeDate = testDate.Substring(testDate.IndexOf(":") + 1).Trim()
Dim possibleFormats = new string() {"d/M/yyyy", "d/M/yyyy H:m", "d/M/yy", "d/M/yy H:m"}
if DateTime.TryParseExact(yourWannaBeDate, possibleFormats, CultureInfo.CurrentCulture, DateTimeStyles.None, dateConverted) then
Console.WriteLine(dateConverted.ToString("yyyy-MM-dd"))
else
Console.WriteLine("Not a date")
End if
End Sub
The above code will match "08/05/2015", "8/5/2015", "08/05/15" and the same strings with the time part in two or one digit format
The trick consists in passing an array of possible formats that could be used to interpret the date in your CurrentCulture (I am assuming that your PC has the culture that match the formats above, however, if this is not the case just initialize a new CultureInfo variable and use it in the TryParseExact)
I wanna thank Leonardo for the tip. This is the code I successfully used:
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("it-IT")
Dim DateResult As DateTime
Dim styles As DateTimeStyles = DateTimeStyles.None
Dim mTmpDate As String
mTmpDate = DatiBase(i).innertext.substring(DatiBase(i).innertext.indexof(":") + 1).trim()
If DateTime.TryParse(mTmpDate, culture, styles, DateResult) Then
MyString = "'" & DateResult.ToString("yyyy-MM-dd HH:mm") & "'"
Else
'Do something else
End If
The code works with any kind of date in "italian style" and in "MyString" I get a date/time formatted as mySql needs (for a timestamp field).
In my application I got error when trying to convert a Date from string date format as shown below:
dateFormat = Format(CDate("2014-mar-06"), "MM/dd/yyyy")
Error
Conversion from string "2014-mar-06" to type 'Date' is not valid
This problem only comes when my Region and Language setting is Spanish(Mexico) (or any spanish but not for others) in Windows 7 . What is the problem and how to solve this?
Avoid VB6 functions like CType and use .NET methods like TryParse instead.
Also CultureInfo.InvariantCulture gets the CultureInfo object that is culture-independent (invariant)
Try this
Dim dateString = "2014-mar-06"
Dim dateValue As DateTime
If DateTime.TryParseExact(dateString, _
"yyyy-MMM-dd", CultureInfo.InvariantCulture, _
DateTimeStyles.None, dateValue) Then
Dim myDate = dateValue.ToString("MM/dd/yyyy") 'Your Date is stored in myDate
Else
'Unable to parse your dateString
End If
I'm trying to convert the date string to "dd/MM/yyyy" format using the code below
m_dEDate = Format(CDate(strExpiry), "dd/MM/yyyy")
But system got the exception error - "parameter is incorrect". If regional short date is "dd-MMM-yy", there is no exception error.
Without setting regional manually, how could we manage to convert by coding?
Should we use System.Globalization? If so, please share me a sample.
use Date.ParseExact
Dim dateString As String = "06/15/2008"
Dim format As String = "MM/dd/yyyy"
Dim provider As CultureInfo = CultureInfo.InvariantCulture
Dim _NewDate As Date = Date.ParseExact(dateString, format, provider)
It looks like you're trying to change the format of a date representation, but you haven't told us what format you're converting from. John's code shows correctly how to parse a value - but it looks like that's the target value you want. Suppose your input is something like "2012-11-15", i.e. yyyy-MM-dd. Then you'd want:
Dim inputText = "2012-11-15"
Dim inputFormat = "yyyy-MM-dd"
Dim outputFormat = "MM/dd/yyyy"
Dim provider = CultureInfo.InvariantCulture
Dim parsedDate = Date.ParseExact(inputText, inputFormat, provider)
Dim outputDate = parsedDate.ToString(outputFormat, provider)
Note that the use of the invariant culture here isolates you completely from culture sensitivity. Is that actually what you want, or do you perhaps want to treat your input according to the user culture, but not the output?