Rafik Bari
Rafik Bari

Reputation: 5037

Unable to convert a string to DateTime?

Inside a function, I need to find the difference between 2 dates in seconds. If the difference is more than 30 seconds i return False otherwise it returns True , first one I read it from database and Second one is the current DateTime.Now

Here is the snippest of code I'm using that does the work while dr.GetValue(0).ToString() holds the current value in the database :

if (dr.Read())
            {

                DateTime nowDate = Convert.ToDateTime(DateTime.Now.ToString("M/dd/yyyy H:mm:ss tt"));
                DateTime then = DateTime.ParseExact(dr.GetValue(0).ToString(), "M/dd/yyyy H:mm:ss tt", CultureInfo.InvariantCulture);

                TimeSpan diff = nowDate - then;

                int timeDifference = diff.Seconds;


                if (timeDifference > 30)
                {
                    myConn.Dispose();
                    return false;
                }
                else {
                    myConn.Dispose();
                    return true;
                }
            }

When i execute the code above i get a message error :
string was not recognized as valid DateTime

And here is the line that is causing the error :

 DateTime then = DateTime.ParseExact(dr.GetValue(0).ToString(), "M/dd/yyyy H:mm:ss tt", CultureInfo.InvariantCulture);

The time is stored in the database in this format : 2013-02-18 14:06:37

But when I execute the following line (for debugging purposes) :

MessageBox.Show(dr.GetValue(0).ToString());

I see a message box that shows the date in this format : 2/18/2013 2:06:37 PM

How to find the difference in seconds between the current time and the time stored in dr.GetValue(0).ToString()

Any help would be highly appreciated

Upvotes: 0

Views: 1540

Answers (4)

Daniel Kelley
Daniel Kelley

Reputation: 7737

Your code really should be very simple:

if (dr.Read())
{
    DateTime then = dr.GetDateTime(0);
    TimeSpan diff = DateTime.Now - then;
    int timeDifference = diff.TotalSeconds;
}

One thing to note - you really shouldn't be calling myConn.Dispose(); in your if/else. Wrap your connection and readers in a using statement.

Upvotes: 1

Rawling
Rawling

Reputation: 50104

You want h, not H. h is the hour in 12-hour format, H is the hour in 24-hour format. Since your example hour is 2 PM (not 14 PM) it's the 12-hour format you want.

Also:

  • You're converting your now-time to a string and back - don't bother!
  • You're counting Seconds not TotalSeconds - this is incorrect because e.g. a 60-second period gives a Seconds value of 0.
DateTime nowDate = DateTime.Now;
DateTime then = DateTime.ParseExact(
    dr.GetValue(0).ToString(), "M/dd/yyyy h:mm:ss tt",
    CultureInfo.InvariantCulture);

TimeSpan diff = nowDate - then;

double secondsDifference = diff.TotalSeconds;

You should even be able to do something along the lines of

DateTime then = dr.GetDateTime(0);

and avoid the string-parsing altogether, but the H/h difference is the reason you get the specific exception you asked about.

Upvotes: 2

Jonny Cundall
Jonny Cundall

Reputation: 2612

if, as it looks like, your date is a datetime in the database, you can probably simplify the two lines to this:

DateTime nowDate = DateTime.Now;
DateTime then = (DateTime)dr.GetValue(0);

(although I'm making a lot of assumptions here)

Upvotes: 1

Hiren Desai
Hiren Desai

Reputation: 1091

I think your server has Application server has some other date format set. What you can try is this:

Convert.ToDate(value,System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormate);

Hope this will solve the error.Haven't tested it so hope for best

Upvotes: 0

Related Questions