Finding the difference between two DateTimes (hours and minutes)

落花浮王杯 提交于 2021-01-29 03:22:13

问题


I'm trying to create a simple program that reads in user input for the time they went to sleep and the time they woke up, and output the total time they slept. The output shows up incorrect and I'm unsure what the problem is. I've tried two different codes but they both seem to be having the same problem.

The first code was this:

Console.WriteLine("Please input time you slept");
string sleepTime = Console.ReadLine();

Console.WriteLine("Please input the time you woke up");
string wakeTime = Console.ReadLine();

TimeSpan sleepDuration = DateTime.Parse(wakeTime).Subtract(DateTime.Parse(sleepTime));

Console.WriteLine("You slept for " + sleepDuration + " hours");
Console.ReadKey();

My first attempt I used 12:00 AM for sleepTime and 8:00 AM for wakeTime. The output was 8 hours, which is correct. I tried changing the values to 11:00 PM for sleepTime and 7:00 AM for wakeTime and the output was -16:00, which is incorrect as the time between 11:00 PM and 7:00 AM is also 8 hours.

This is the second code I tried using:

Console.WriteLine("Please input the time you went to sleep");
DateTime sleepTime = Convert.ToDateTime(Console.ReadLine());

Console.WriteLine("Please input the time you woke up");
DateTime wakeTime = Convert.ToDateTime(Console.ReadLine());

TimeSpan sleepDuration = wakeTime - sleepTime;

Console.WriteLine("You slept for " + sleepDuration + " hours");
Console.ReadKey();

I used the exact same times as I did for the first code and got the exact same outputs. 12:00 AM to 8:00 AM outputs 8:00 and 11:00 PM to 7:00 AM outputs -16:00.

What's causing -16:00 to be the output and how can I fix it?


回答1:


The problem here is that parsing only a time with DateTime.Parse defaults the date portion to DateTime.Today.

If you input that you went to bed at 23:00 and woke up at 07:00 these values will look like this (for today when I write this answer):

2016-01-20 23:00
2016-01-20 07:00

Notice it's the same date. DateTime.Parse has no knowledge of the context in which you're parsing values and it shouldn't.

Basically you "slept in reverse".

You should handle this like this:

if (wakeTime < sleepTime)
    wakeTime = wakeTime.AddDays(1);

This will correctly represent the fact that you woke up the following morning, then subtraction will correctly return 8 hours.




回答2:


If wakeTime < sleepTime then wakeTime is obviously the next day. You then need to add another day:

 if (sleepDuration.TotalHours < 0)
     sleepDuration += TimeSpan.FromDays(1);



回答3:


the problem while using the date time parsing using Date only you must give date to get the exact solution other wise

TimeSpan sleepDuration=null;
if(wakeTime> sleepTime)
   sleepDuration=  wakeTime - sleepTime;
else
sleepDuration=   sleepTime-wakeTime;


来源:https://stackoverflow.com/questions/34893161/finding-the-difference-between-two-datetimes-hours-and-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!