1 year ago
#314562
dillion n
c# Calculate time remaining until certain time
So I am trying to find the time that is closest to happening from a list of three times. Then I am trying to calculate the time remaining until the closest time happens. My code to find the closest time works but my timeleft code only works when the chosen closest time is either 2pm, or 10pm. The three times I am checking to see what are closest is 6am, 2pm, or 10pm. But if it is after 10pm instead of saying 8 hours til 6 am it will say -16hours. If anyone can help would be greatly appreciated!
Sample Input: 11pm Expect output: 7 (11 pm would mean it is 7 hours until 6 am)
Here is my time left code:
public static TimeSpan timeLeft(TimeSpan ts)
{
TimeSpan time = new TimeSpan(0);
DateTime next = DateTime.Now;
next = next.AddMinutes(-next.Minute).AddSeconds(-next.Second) + ts;
if (next < DateTime.Now)
next = next.AddHours(1);
return next - DateTime.Now;
}
c#
timespan
0 Answers
Your Answer