Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a label that get a returned value as datetime.

i want to state a condition that will look at the time and perform an action if the datetime is more than/greater than 30 min as show in the code below.

thank you.

What I have tried:

C#
lblCompletedLastTime.Text ="Last Completed at "+ dt.Rows[0]["Last_TimeCompleted"].ToString(); // this gets the datetime from the database and returns it to the label
                if (lblCompletedLastTime.Text > 30min)// if the returned datetime is greater than 30min
                {
                   to do xxxxxx
                }
Posted
Updated 28-Oct-16 6:09am

Assuming you mean "if the time is more than 30 minutes ago", and assuming you're storing the value in the database as either datetime or datetime2, then something like this should work:
C#
DateTime lastCompleted = (DateTime)dt.Rows[0]["Last_TimeCompleted"];
lblCompletedLastTime.Text = string.Format("Last Completed at {0}", lastCompleted);

if (lastCompleted.ToUniversalTime().AddMinutes(30) < DateTime.UtcNow)
{
    // Perform the action...
}
 
Share this answer
 
Comments
Emmablakes 28-Oct-16 12:45pm    
thank you.
There exists the TimeSpan structure which is exactly what you need:
C#
DateTime completedOn = dt.Rows[0]["Last_TimeCompleted"];
TimeSpan elapsed = DateTime.Now - completedOn;

if (elapsed.TotalMinutes > 30)
{
   // TODO
}

As you can see, you can subtract two DateTime structures and get a TimeSpan structure characterizing the time-difference between them.

Kindly.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900