Dharmendra Kumar Singh
Dharmendra Kumar Singh

Reputation: 2991

Finding the greatest datetime from the given datetime in c#

I am storing information of a associate production and nonproduction details in the Database(SQL Server).My task is that, if their is any record in the database for the current date(either in production table or non production table or both),get the Task Submitted Time from both table .I had already done up to this point.

if ((_timeEntryId > 0) || (_timeEntryIDNonProduction > 0))
{
    if (_timeEntryId > 0)
    {
        DateTime lastProductiontaskTIme = _production.GetLastTaskTime(Convert.ToInt32(_timeEntryId));
    }

    if (_timeEntryIDNonProduction > 0)
    {
        DateTime lastNonProductionTime= _nonProduction.GetLastTaskTime(Convert.ToInt32(_timeEntryId));
    }
}

Now my requirement is to identify the greatest DateTime (Most Current) from lastProductiontaskTIme & lastNonProductionTime .How i found the most recent from both. Please help me in this issue .

Upvotes: 0

Views: 112

Answers (2)

Chris Gessler
Chris Gessler

Reputation: 23113

You can use Math as well:

lastTime = new DateTime(Math.Max(lastProductiontaskTime.Ticks, lastNonProductionTime.Ticks)) 

Upvotes: 1

Bas
Bas

Reputation: 27095

DateTime lastTime;
if ((_timeEntryId > 0) || (_timeEntryIDNonProduction > 0))
{
    DateTime lastProductiontaskTime;
    if (_timeEntryId > 0)
    {
        lastProductiontaskTime = _production.GetLastTaskTime(Convert.ToInt32(_timeEntryId));
    }
    DateTime lastNonProductionTime;
    if (_timeEntryIDNonProduction > 0)
    {
        lastNonProductionTime = _nonProduction.GetLastTaskTime(Convert.ToInt32(_timeEntryId));
    }
    lastTime = lastProductiontaskTime > lastNonProductionTime  ? lastProductiontaskTime  : lastNonProductionTime;
}

This gets the highest value of the two. DateTime structures can be compared like integers and the highest value is later in time.

Upvotes: 0

Related Questions