CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Posts
    27

    Comparing Time in C# and MySQL

    Hello everyone
    I'm writing something for fun using both C# and MySQL.

    To make things short I want to check if a certain user registered more than 24 hours ago.

    I have made a collumn in my table "users" called "timereg", indicating the time they registered. The values of the collumn are as follows:
    Code:
    `timereg` timestamp NOT NULL DEFAULT 'CURRENT_TIMESTAMP',
    Now I want to create a function to compare the timestamp in the database and the current time (using DateTime)
    here is my function
    Code:
    public static void check(string username) 
            {
               
                Server.Database.RunQuery("SELECT `timereg` FROM users WHERE name = '" + username + "'");
                MySqlDataReader data = Server.Database.Reader;
                if (data.GetDateTime("timereg") > DateTime.Now.TimeOfDay)
                {
                    //whatever here
                }
                else if (data.GetTimeSpan("timereg") < DateTime.Now.TimeOfDay)
                {
                    //whatever here
                }
                    data.Close(); 
                
            }
    Now, the problem is that I don't know how to compare the two. Could anyone help me with what function I should use to compare the two? In other words, instead of "data.GetTimeSpan", is there another function that I can use?
    Thanks.

  2. #2
    Join Date
    Apr 2012
    Posts
    29

    Re: Comparing Time in C# and MySQL

    DateTime constructors include
    DateTime(year, month, day, hour,minute,second,...);
    Depends on the format of the mysql datetime you retrieve, parse it as another datetime then comparing it with DateTime.Now is also another option.

  3. #3
    Join Date
    May 2012
    Posts
    27

    Re: Comparing Time in C# and MySQL

    Quote Originally Posted by thefollower View Post
    DateTime constructors include
    DateTime(year, month, day, hour,minute,second,...);
    Depends on the format of the mysql datetime you retrieve, parse it as another datetime then comparing it with DateTime.Now is also another option.
    Thank you! I ended up comparing DateTime.Now and the date from the database (+24 hours)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured