Click here to Skip to main content
15,669,040 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I am having an issue converting a string(textbox1.text)to date/time short string.

'Dim today As System.DateTime
Dim future As System.DateTime
' String to DateTime
Dim MyString As [String]
MyString = txtdate.Text
'MyString = "02/02/2011";  
Dim MyDateTime As DateTime
MyDateTime = New DateTime()
MyDateTime = DateTime.ParseExact(MyString, "dd/mm/yyyy", Nothing)
future = MyDateTime.AddDays(txtnos.Text)
txtenddate.Text = future

Im not to sure what is wrong with the above code, it works fine but does not return the correct string value. instead of adding the number of days to the Mystring, it is starting from the start of the year and then counting the number of days....i believe it is something to do with the datetime.parseextract formatting.

Any help would be great.
Updated 23-May-11 18:20pm

Use the following

MyDateTime = DateTime.ParseExact(MyString, "dd/MM/yyyy", Globalization.CultureInfo.InvariantCulture)
Share this answer
SIFNOk 24-May-11 0:21am    
thank you very very very much, this has been troubling me for dayss!!!
Your the best!
Prerak Patel 24-May-11 0:22am    
You are welcome.
I have same problem but i solved by using IFormatProvider
Dim provider as IFormatProvider
provider = new system.globalization.cultureinfo("en-GB",true)
dim future as system.datetime
dim Mystring as string
dim mydatetime as datetime
mydatetime = new datetime()
Share this answer
SIFNOk 24-May-11 0:55am    
Thanks Jwalia For your response.
This retrieved the same result as the below solution! but i thank you, as this will show me alternative methods and also how its strutured. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900