Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
tring dateFromformat = "dd/MM/yyyy";
DateTime dateFrom = DateTime.ParseExact(tb_DateFrom.Text, dateFromformat, CultureInfo.InvariantCulture);

//error is this -
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
Posted
Comments
TrushnaK 31-Jan-14 3:34am    
which date you enter in textbox.. and what type of output you want.
BillWoodruff 31-Jan-14 3:35am    
What's an example of the content of tb_DateFrom.Text when you get the error ?
CPallini 31-Jan-14 3:37am    
You have to handle incorrect user input, haven't you?
Member 10548723 31-Jan-14 3:39am    
The date I entered was 21/12/2014

This is the error - String was not recognized as a valid DateTime.
Richard MacCutchan 31-Jan-14 3:46am    
I just tried that value against your code and it worked. Check that the string you enter is not being correpted somewhere.

1 solution

I tried that code, with the date string you describe:
C#
string dateFromformat = "dd/MM/yyyy";
DateTime dateFrom1 = DateTime.ParseExact("21/12/2014", dateFromformat, CultureInfo.InvariantCulture);
DateTime dateFrom2 = DateTime.ParseExact("12/21/2014", dateFromformat, CultureInfo.InvariantCulture);
I get no error with the first conversion, but I do get a "Gregorian Calendar" exception when the second executes.

Did you perhaps miskey the input string?

(BTW: I'd use TryParseExact, instead of ParseExact, and report an error to the user instead of relying on the exception, or by preference use a DateTimePicker control instead of a TextBox - that way the user can't enter a wrong value and you get the DateTime directly without any conversions)
 
Share this answer
 
Comments
Member 10548723 31-Jan-14 3:51am    
I am not sure why also.
Member 10548723 31-Jan-14 3:55am    
This how i store in my db

cmd.Parameters.AddWithValue("@dateFrom",Convert.ToDateTime("dd/MM/yyyy",CultureInfo.InvariantCulture));
OriginalGriff 31-Jan-14 4:18am    
Well, that looks nothing like the code you started off telling us about, and will always fail - because it uses a fixed string as the date which is not a valid date...you can't supply any format strings to Convert.ToDateTime (there isn't an overload which accepts one)

What are you trying to tell me?

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