Reputation: 6394
I have the following Code:
var enteredDate = Convert.ToDateTime("17:45");
var todaysDateTime = DateTime.Now;
var span = enteredDate.Subtract(todaysDateTime);
double totalMins = Math.Ceiling(span.TotalMinutes);
string timeCond;
if (totalMins > 0)
{
if (totalMins < 5)
{
timeCond = Math.Ceiling(span.TotalSeconds) + " seconds left.";
}
else
{
timeCond = totalMins + " minutes left.";
}
}
Given that the time now would be 17:50
the returned second would be a negative figure, I would like to be able to return the seconds or minutes in relation to the code for the next time the time would be 17:45
, is this possible?
Upvotes: 1
Views: 276
Reputation: 70369
try
var span = Convert.ToDateTime("17:45") > DateTime.Now ? Convert.ToDateTime("17:45") - DateTime.Now : Convert.ToDateTime("17:45").AddDays(1) - DateTime.Now
Upvotes: 0
Reputation: 1500893
You could always just add a day:
var span = enteredDate - todaysDateTime;
if (span < TimeSpan.Zero)
{
span += TimeSpan.FromDays(1);
}
(Note that this assumes there are 24 hours between today's 17:45 and tomorrow's 17:45. That isn't true around daylight saving transitions; accommodating for that is feasible, but would make life somewhat more complicated.)
Upvotes: 3