简体   繁体   中英

Check if 1 hour has passed from SQL Server Datetime column string C#

I'm trying to check if 1 hour ha passed from string Datetime that I have in SQL Server database. I tried this code but it didn't work or I didn't understand it right.

What I'm trying to do is to take the column Datetime value and check if passed 1 hour from TimeDate.Now .

Here is my code :

using (var GetLastPost = new SqlCommand("SELECT PostDate FROM Accs2 WHERE Username LIKE @user1", con))
{
    GetLastPost.Parameters.AddWithValue("@user1", ecid);

    using (var LastReader = await GetLastPost.ExecuteReaderAsync())
    {
        if (!LastReader.HasRows || !LastReader.Read())
        {
        }
        else
        {
            DateTime k = Convert.ToDateTime(LastReader["PostDate"].ToString());

            if (k > DateTime.Now)
            {
                MessageBox.Show("Your Money is in the Box");
            }
            else
            {
                MessageBox.Show("1 Hour Not Passed yet");
                con.Close();
            }
        }
    }
}

A simple method would be something like

public bool HasAnHourPassed(DateTime compareTime)
{
    var timePassed = DateTime.Now - compareTime;
    return timePassed.TotalHours > 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM