Parsing strings into DateTime object in C#

In the attached code script , from date is being working perfectly fine, but on parsing todate, it is saying that The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

The syntax and format for both the dates are exactly the same ,Its giving me quite a bad headache.

Urgently required
string fromdate = txtFromdate.Text.ToString();
            string todate = txtToDate.Text.ToString();
            if(fromdate != "")
            {
                fromdate = DateTime.ParseExact(txtFromdate.Text.ToString(), "dd.MM.yyyy hh:mm:ss", null).ToString("MM/dd/yyyy hh:mm:ss");
            }
            
            if (todate != "")
            {
               todate = DateTime.ParseExact(txtToDate.Text.ToString(), "dd.MM.yyyy hh:mm:ss", null).ToString("MM/dd/yyyy hh:mm:ss");
               
            }
            else if (todate == "")
            {
                DateTime current = DateTime.Now;
                todate = current.ToString();
            }

Open in new window

LVL 1
Ammar IqbalSenior IT Consultant/senior Software engineerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Richard LeeConnect With a Mentor Software EnthusiastCommented:
Try switching to 24.11.2010 08:10:20

DaTribe
0
 
Richard LeeSoftware EnthusiastCommented:
What dates are you passing to this routine. Could you post the exact string so that the issue could be replicated.

DaTribe
0
 
Erick37Commented:
Are you sure the input is in the correct format?

Passing in a date "12.13.2010 12:12:12" will throw that error -- the month and day are transposed.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:


 From date  is this "11/10/2010 11:04:40" which is perfectly fine
Todate is this  "11.24.2010 08:10:20".. it  is giving this exception

0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
I have given the parsed result of fromdate, and todate is unsuccessfull in parsing
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
i am going nuts
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
i want to have this pattern of time with "/" in it , because i need to parse in the filter criteria for the sql query, and the datetime fields of sql query recognize "/" slashes
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
ok
0
 
Ammar IqbalSenior IT Consultant/senior Software engineerAuthor Commented:
Got it.. It was  fool of me , that i set the format of todate as MM.dd, and iam trying over here as dd.mm

thankx  for looking into it
0
 
Richard LeeSoftware EnthusiastCommented:
Simply replace the . with /
0
 
kris_perCommented:

Try changing  dd.MM.yyyy to =>  MM.dd.yyyy like:
...
if(fromdate != "")
            {
                fromdate = DateTime.ParseExact(txtFromdate.Text.ToString(), "MM.dd.yyyy hh:mm:ss", null).ToString("MM/dd/yyyy hh:mm:ss");
            }
            
            if (todate != "")
            {
               todate = DateTime.ParseExact(txtToDate.Text.ToString(), "MM.dd.yyyy hh:mm:ss", null).ToString("MM/dd/yyyy hh:mm:ss");
               
            }
...

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.