Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parsing strings into DateTime object in C#

Posted on 2010-11-29
11
Medium Priority
?
2,510 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

876 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