I have used a textbox to store a date in DD-MM-YYYY format and since i use SQL server开发者_StackOverflow中文版 i used to get the value from the textbox and then place it in a variable of type DateTime dt
DateTime dt = DateTime.ParseExact(TextBox10.Text,"MM-DD-YYYY",CultureInfo.InvariantCulture);
I am getting format specification error, I went through many documentation but still couldn't get where i am going wrong
This i am using C# and ADO.net Please can anyone correct me?
It looks like you mixed up DD and MM. Try below:
DateTime dt = DateTime.ParseExact(TextBox10.Text,"dd-MM-yyyy",CultureInfo.InvariantCulture);
Notice that I changed "DD" to "dd"!
UPDATE:
Ok, so I changed "YYYY" to "yyyy" and ran the code below and it parsed sucessfully:
DateTime dt = DateTime.ParseExact("28-01-2010", "dd-MM-yyyy", provider);
If your users really are passing in dates in the format "dd-MM-yyyy" this should work for you. Keep in mind that the second string has nothing to do with whatever format you are storing in your database. It only has to do with the format of the first string argument in ParseExact. HTH.
try this
DateTime.ParseExact("12-12-2010", "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture)
Take care of date format string casing. "dd" can be different from "DD"
As mentioned above, date format string is case sensitive.
However, are you sure you want to use DateTime.ParseExact as opposed to DateTime.Parse?
精彩评论