Solved

Parsing strings into DateTime object in C#

Posted on 2010-11-29
11
2,298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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