Solved

Parsing strings into DateTime object in C#

Posted on 2010-11-29
11
2,146 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now