locked
String was not recognized as a valid DateTime - VB.net RRS feed

  • Question

  • I'm creating a webform which have 1 textbox(TextBox1) for Date that gets the date value from a calendar control, and 3 dropdownlists control for hour(timeHour), minute(timeMin) and am/pm(timeIn). I tried using DateTime.Parse method but it didnt work. Can anybody please help point out my mistake?

    Coding:

    Dim dDate As Date = TextBox1.Text

    Dim dt As Date = DateTime.Parse(timeHour.SelectedValue.Trim() + ":" + timeMin.SelectedValue.Trim() + ":00 " + timeIn.SelectedValue)

    Dim dTime As Date = dt

    Dim d As New Date(dDate.Year, dDate.Month, dDate.Day, dTime.Hour, dTime.Minute, dTime.Second, DateTimeKind.Local)

    ============================

    Error:

    [FormatException: String was not recognized as a valid DateTime.]

    ============================


    Wednesday, October 5, 2016 4:37 AM

Answers

  • You should probably use the DateTime.TryParse Method (String, DateTime) method on the TextBox with dDate rather than providing dDate with TextBox text which is not a DateTime. DateTime.TryParse will return a boolean value if it works or not.

    If it does work then dDate will contain the parsed DateTime. If not then stop your code and display an error somewhere I suppose.


    La vida loca

    • Proposed as answer by Neda Zhang Thursday, October 13, 2016 5:41 AM
    • Marked as answer by Neda Zhang Thursday, October 13, 2016 5:41 AM
    Sunday, October 9, 2016 1:15 AM

All replies

  • Hi NurulShaf,

    Since it is related to the VB language development, I help you move this case to the VB forum for dedicated support.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 6, 2016 3:17 AM
  • Hi

    I am not sure about my answer, but if you use a DateTimePicker, with a Custom Format, the user could select both Date and Time in one control - the value would then be a DateTime type which you could use in any way quite easily, separating out any part of the DateTime, manipulate the value in any way and generally use quite easily,

    I might not be on the right track as I have never used/coded a Web Form - I am assuming that A DateTimePicker control is usable on that.

    *

    If that is of use to you, I could explain in more detail on how to use it.


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, October 6, 2016 3:32 AM
    Thursday, October 6, 2016 3:30 AM
  • [FormatException: String was not recognized as a valid DateTime.]

    If the error occurs at this line

        Dim dDate As Date = TextBox1.Text

    then it is telling you that whatever is in the text box cannot be recognised as a date.  You need to either change the textbox contents so it is a valid date, or provide a custom date format for the date parser that matches the text box contents, or both.

    If the error occurs somewhere else you should indicate what line of code it is and the values of the variables used in that line.

    If this is an ASP.Net application the question should be asked in the ASP forum: ASP.Net forums

    Thursday, October 6, 2016 3:44 AM
  • You should probably use the DateTime.TryParse Method (String, DateTime) method on the TextBox with dDate rather than providing dDate with TextBox text which is not a DateTime. DateTime.TryParse will return a boolean value if it works or not.

    If it does work then dDate will contain the parsed DateTime. If not then stop your code and display an error somewhere I suppose.


    La vida loca

    • Proposed as answer by Neda Zhang Thursday, October 13, 2016 5:41 AM
    • Marked as answer by Neda Zhang Thursday, October 13, 2016 5:41 AM
    Sunday, October 9, 2016 1:15 AM
  • Hello,

    You should use TryParse since the value is coming from a TextBox. Better is to use a DateTimePicker or if this is web based SPA using jQuery widget to get the date.

    https://msdn.microsoft.com/en-us/library/ch92fbc1%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, October 9, 2016 5:51 AM