I am trying to convert my string formatted value to date type with format dd/MM/yyyy
.
this.Text="22/11/2009";
DateTime date = DateTime.Parse(this.Text);
What is the problem ?
It has a second override which asks for IFormatProvider
. What is this? Do I need to pass this also? If Yes how to use it for this case?
Edit
What are the differences between Parse
and ParseExact
?
Edit 2
Both answers of Slaks and Sam are working for me, currently user is giving the input but this will be assured by me that they are valid by using maskTextbox.
Which answer is better considering all aspects like type saftey, performance or something you feel like
Just like someone above said you can send it as a string parameter but it must have this format: '20130121' for example and you can convert it to that format taking it directly from the control. So you'll get it for example from a textbox like:
to convert it to: '20130121' you use:
so that SQL can convert it and put it into your database.
You need to call
ParseExact
, which parses a date that exactly matches a format that you supply.For example:
The
IFormatProvider
parameter specifies the culture to use to parse the date.Unless your string comes from the user, you should pass
CultureInfo.InvariantCulture
.If the string does come from the user, you should pass
CultureInfo.CurrentCulture
, which will use the settings that the user specified in Regional Options in Control Panel.Although the above solutions are effective, you can also modify the webconfig file with the following...
Ref : Datetime format different on local machine compared to production machine
use this to convert string to datetime:
You might need to specify the culture for that specific date format as in:
For more details go here:
http://msdn.microsoft.com/en-us/library/5hh873ya.aspx
After spending lot of time I have solved the problem