Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9311383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:21:01+00:00 2026-06-19T01:21:01+00:00

Inside a function, I need to find the difference between 2 dates in seconds

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:21:03+00:00Added an answer on June 19, 2026 at 1:21 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside a function I need to test if another variable function exists and then
I need to use a function inside of another function, how can I do
I have a bit of a quandary. I need to call a function inside
What I want to do is following. Inside a function, I need to assign
I need a function to get the height of the highest element inside a
Inside a bash script function, I need to work with the command-line arguments of
I need to call a JS function with a C# parameter. This is inside
I am building a common function library but the functions inside need to reference
Inside of a static member function I need to get the type. class MyClass
I need to find if a numpy array is inside other numpy array, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.