converting a string to date  
Author Message
Can-Ann





PostPosted: Visual C# Language, converting a string to date Top

I have a string that contains the a date in the format of "dd/mm/yyyy".

I want to convert this to a datetime MyDate

Can anyone show me the best way of doing this.



Visual C#13  
 
 
RizwanSharp





PostPosted: Visual C# Language, converting a string to date Top

See this function "Convert.ToDateTime()"

Best Regards,

Rizwan



 
 
Can-Ann





PostPosted: Visual C# Language, converting a string to date Top

This is the line that is causing the error:

this.sudetail .SuppF12 = Convert .ToDateTime (cRow [11].ToString ());

cRow[11] is equal to "08/11/2004" and is a string

This is the assessor

private DateTime F12SuppDateOpened;

public DateTime SuppF12 //date opened

{

get

{

return this.F12SuppDateOpened;

}

set

{

this.F12SuppDateOpened = value ;

}

}


 
 
Felan





PostPosted: Visual C# Language, converting a string to date Top

Try

this.sudetail .SuppF12 = Convert .ToDateTime (cRow [11] as string);

I'm not sure what cRow is but ToString doesn't really convert one type of thing to another type of thing. Exactly what ToString() does is up to the maker of the class.


 
 
ahmedilyas





PostPosted: Visual C# Language, converting a string to date Top

what is the exact error you are getting

you can use DateTime.ParseExact() to parse it to the exact date you want however there still maybe an exception thrown if the arguments are invalid.

DateTime myDateTime = DateTime.ParseExact(cRow[11].ToString(), "dd/MM/yyyy", new System.Globalization.CultureInfo("cultureHere"));

http://msdn2.microsoft.com/en-us/library/w2sa9yss.aspx



 
 
Can-Ann





PostPosted: Visual C# Language, converting a string to date Top

The error is a 'Format Error' - and cRow[11] is a datarow.

Felan - I tried your 'as string' but it also gave the same error.

This seemed to work

DateTime myDateTimeA = DateTime.ParseExact(cRow[11].ToString().Replace ("-","/"), "dd/MM/yyyy", new System.Globalization.CultureInfo("en-US"));

Thanks guys for your help.


 
 
ashk1860





PostPosted: Visual C# Language, converting a string to date Top

hi dear friends

I think there have to be some sulotions to make a program independet from the culture of the computer which runs the application.

I had this problem with the numbers also. because the users that use my program might use different kind of cultures and I use thread to solve it but I am not sure it is a best way or not.

regards