Solved

Parsing strings into DateTime object in C#

Posted on 2010-11-29
11
2,231 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:Ammar Iqbal
11 Comments
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34231572
What dates are you passing to this routine. Could you post the exact string so that the issue could be replicated.

DaTribe
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34231660
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
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34231719


 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34231727
I have given the parsed result of fromdate, and todate is unsuccessfull in parsing
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34231730
i am going nuts
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
ID: 34231738
Try switching to 24.11.2010 08:10:20

DaTribe
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34231747
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
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34231750
ok
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34231772
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
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34231778
Simply replace the . with /
0
 
LVL 16

Expert Comment

by:kris_per
ID: 34231786

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question