Click here to Skip to main content
15,886,734 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.

VB
'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.
Posted
Updated 23-May-11 18:20pm
v2

Use the following

VB
MyDateTime = DateTime.ParseExact(MyString, "dd/MM/yyyy", Globalization.CultureInfo.InvariantCulture)
 
Share this answer
 
Comments
SIFNOk 24-May-11 0:21am    
PRERAK YOU ARE GOD! :D
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
mystring=txtdate.text
dim mydatetime as datetime
mydatetime = new datetime()
mydatetime=date.parse(mystring,provider,system.globalization.datetimestyles.nocurrentdatedefault)
 
Share this answer
 
v2
Comments
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