30

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?

2
  • 1
    Please clarify - are you using VB6 or VB.NET?
    – Oded
    Dec 26, 2011 at 9:54
  • Sorry i have corrected my question
    – Ishan
    Dec 26, 2011 at 10:00

5 Answers 5

65

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)
1
  • Thank You.When i debug the expenddt has the vale #12/31/2011# when edate value is 31/12/2011. Why is it so? Why the format gets changed?
    – Ishan
    Dec 27, 2011 at 4:56
7

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 );
5

Try to see if the following code helps you:

Dim iDate As String = "05/05/2005"
Dim oDate As DateTime = Convert.ToDateTime(iDate)
0
3

Try converting date like this:

    Dim expenddt as Date = Date.ParseExact(edate, "dd/mm/yyyy", 
System.Globalization.DateTimeFormatInfo.InvariantInfo);

Hope this helps.

1
  • 8
    I think it should be "dd/MM/yyyy". mm is for the minutes.
    – mbomb007
    Oct 12, 2015 at 18:57
2

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

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.