Conversion from string to type 'Date' is not valid

Posted by Thiru under ASP.NET on 9/15/2011 | Points: 10 | Views : 29384 | Status : [Member] | Replies : 16
Hi


Dim dob As DateTime
dob = CType(Year(txtdob.Text) & "-" & Month(txtdob.Text) & "-" & Day(txtdob.Text), Date)


if txtdob.text="09/02/2011" --- above code is working fine.
if txtdob.text="9/22/2011" --- above code is showing error: ex = {"Conversion from string "9/22/2011" to type 'Date' is not valid."}

Why and how to over come from this.

2nd question is:
And i need to know how to format it like: yyyy-MM-dd ?




Responses

Posted by: Vuyiswamb on: 9/15/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
try this


Dim dob As DateTime

dob = Convert.ToDateTime("01" & "-" & "01" & "-" & "2011")


Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 9/15/2011 [Member] Starter | Points: 25

Up
0
Down
I used:

dob = Convert.ToDateTime(Year(txtdob.Text) & "-" & Month(txtdob.Text) & "-" & Day(txtdob.Text))

but same error

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/15/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
no show me whatdoes each textbox pass , show me the values as i did with the examples

Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 9/15/2011 [Member] Starter | Points: 25

Up
0
Down
value in txtdob.text is 9/22/2011
dob = Convert.ToDateTime(txtdob.Text)
showing: ex = {"String was not recognized as a valid DateTime."}


Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/15/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
i did this

     Dim dob As DateTime


dob = Convert.ToDateTime("9/22/2011")
Response.Write(dob)


and its working, your Date is not what you say it is , debug that and check the Date that is passed , it is incorrect.



Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 9/15/2011 [Member] Starter | Points: 25

Up
0
Down
i am getting the same error: "String was not recognized as a valid DateTime."
note: txtdob.text value is "9/25/2011"

Dim dob as DateTime

dob = Convert.ToDateTime(txtdob.Text)


Need to have a solution please.


Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/15/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
reply to this post with an Example project

Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 9/15/2011 [Member] Starter | Points: 25

Up
0
Down
Dim dob As DateTime

dob = Convert.ToDateTime(txtdob.Text)

its showing and error: "String was not recognized as a valid DateTime."
even i created a new page and use the same code - its not working in this project.

but its working fine - if i copy and paste it in another test project.

Really confused with this and wasting my day today.
i cant able to move further without this...

need to have a solution or any other bypass / any other method to over come
Expecting valuable suggestion - please.



Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 9/15/2011 [Member] Starter | Points: 25

Up
0
Down
Full code:

Public Partial Class WebForm4
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub Signup_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Signup.Click
Try
Dim dob As DateTime
dob = Convert.ToDateTime(txtdob.Text)
Catch ex As Exception

End Try
End Sub
End Class


Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/15/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
I will TeamView to your Computer later , just check if i am online.

Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 9/15/2011 [Member] Starter | Points: 25

Up
0
Down
My God Thanks for a great suggestion.
Waiting for you. At what time may i expect you?

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gausi on: 9/15/2011 [Member] Starter | Points: 25

Up
0
Down
i try this in c#
 var t= DateTime.Parse("9/22/2011");


it works and also to Format it in "yyyy-MM-dd"

var t= DateTime.Parse("9/22/2011").ToString("yyyy-MM-dd");


"String was not recognized as a valid DateTime." this error most often get when there is month in date greater than 12.




Regards,

Gaurav Singh
(gausi)

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 9/15/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks a lot Vuyiswamb,

Thanks for spending your valuable time through TeamViewer in my computer.

The problem we found is with the date format of my computer.
after changing the regional setting in my computer as yyyy-MM-dd
its working fine.

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/15/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Thank you for posting at Dotnetfunda

Please mark your post as Resolved.

Vuyiswa Maseko


Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/16/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
:)


Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 6/14/2012 [Member] Bronze | Points: 25

Up
0
Down
DateTime date = DateTime.ParseExact(text, "dd/MM/yyyy",CultureInfo.InvariantCulture);
string reformatted = date.ToString("yyyyMMdd", CultureInfo.InvariantCulture);

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response