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?
Try to see if the following code helps you:
You should have to use
Date.ParseExact
orDate.TryParseExact
with correct format string.OR
OR
Try converting date like this:
Hope this helps.
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
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