找出两个DateTime之间的时差(小时和分钟) [英] Finding the difference between two DateTimes (hours and minutes)

查看:53
本文介绍了找出两个DateTime之间的时差(小时和分钟)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的程序,以读取用户输入的睡眠时间和唤醒时间,并输出睡眠的总时间.输出显示不正确,我不确定是什么问题.我尝试了两种不同的代码,但是它们似乎都遇到了相同的问题.

第一个代码是这样的:

  Console.WriteLine(请输入您睡觉的时间");字符串sleepTime = Console.ReadLine();Console.WriteLine(请输入您起床的时间");字符串wakeTime = Console.ReadLine();TimeSpan sleepDuration = DateTime.Parse(wakeTime).Subtract(DateTime.Parse(sleepTime));Console.WriteLine(您睡了" + sleepDuration +小时");Console.ReadKey(); 

我的第一次尝试是将12:00 AM用于睡眠时间,将8:00 AM用于唤醒时间.输出为8小时,这是正确的.我尝试将sleepTime的值更改为11:00 PM,将akeTime的值更改为7:00 AM,并且输出为-16:00,这是不正确的,因为11:00 PM和7:00 AM之间的时间也是8小时./p>

这是我尝试使用的第二个代码:

  Console.WriteLine(请输入您进入睡眠的时间");DateTime sleepTime = Convert.ToDateTime(Console.ReadLine());Console.WriteLine(请输入您起床的时间");DateTime唤醒时间= Convert.ToDateTime(Console.ReadLine());TimeSpan sleepDuration =唤醒时间-sleepTime;Console.WriteLine(您睡了" + sleepDuration +小时");Console.ReadKey(); 

我使用与第一个代码完全相同的时间,并获得了完全相同的输出.从12:00 AM到8:00 AM输出8:00,从11:00 PM到7:00 AM输出-16:00.

是什么导致-16:00成为输出,我该如何解决?

解决方案

此处的问题是,仅使用 DateTime.Parse 解析时间会将日期部分默认为 DateTime.Today .

如果您输入您在23:00上床睡觉并在07:00醒来,那么这些值将如下所示(对于今天,当我编写此答案时):

 <代码> 2016-01-20 23:002016-01-20 07:00 

请注意,这是同一日期. DateTime.Parse 不了解您在其中解析值的上下文,因此不知道.

基本上,您是反向睡觉".

您应该这样处理:

  if(wakeTime< sleepTime)唤醒时间=唤醒时间.AddDays(1); 

这将正确表示您第二天早上醒来的事实,那么减法将正确返回8个小时.

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?

解决方案

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.

这篇关于找出两个DateTime之间的时差(小时和分钟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆