如何使用DateTimePickers找到输入的开始时间和几个小时的结束时间之间的区别?视觉工作室C#

发布于 2025-02-04 07:54:36 字数 70 浏览 2 评论 0原文

我想使用C#Windows表单应用程序中的日期时间选择器使用C#找到输入的开始时间和结束时间之间的区别。输出应在数小时内给出。

I want to find the difference between the entered Start Time and End Time using date-time pickers in C# windows forms applications using c#. The output should be given in hours.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末蓝 2025-02-11 07:54:36

获取两个dateTimePipters并相应地命名。然后转到属性窗口,然后将其格式更改为“时间”。下面的代码是用于触发计算并在文本框中显示输出的按钮。

    private void btnClickMe_Click(object sender, EventArgs e)
    {
        TimeSpan diff = dtpEndTime.Value - dtpStartTime.Value;
        int hours = diff.Hours + 1;
        txtShowOutput.Text = Convert.ToString(hours);
    }

Get two DateTimePickers and name them accordingly. Then go to property window and change their format to ”Time”. Below code is for the button which will trigger the calculation and show the output in a textbox.

    private void btnClickMe_Click(object sender, EventArgs e)
    {
        TimeSpan diff = dtpEndTime.Value - dtpStartTime.Value;
        int hours = diff.Hours + 1;
        txtShowOutput.Text = Convert.ToString(hours);
    }
千紇 2025-02-11 07:54:36

首先,接受的答案是错误的。让我们假设两个dateTimePickers包含相同的日期,而dtpstarttime的时间为01:00:00(恰好是早晨的一个钟)和dtpendtime时间为07:00:00(早餐时间)。这两次都在整个几秒钟都降落,因此在此示例中,我们不必关心一秒钟的分数。

那么,01:00:00至07:00:00之间的时差是什么?正是六个小时。然而,显然,被接受的答案中的代码产生了不正确的时间差,显然是七个小时。那么,为什么要接受这个答案呢?我只能猜测,询问者更喜欢有时会获得不正确的结果,而不是始终正确的结果。

给出一个更有意义的答案,首先需要决定如何处理并非整个时间的时间差异,例如01:15:00和03:07:47之间的差异, 例如。这个问题没有给出明确的指示,因此我在这里填充空白并在此处定义自己的规则:如果时间差的时间差为> = 30分钟,否则,时间差应将 down 舍入到下一个较小的小时。基本上是:

TimeSpan diff = dtpEndTime.Value - dtpStartTime.Value;
int hours = (int) (Math.Abs(diff.TotalHours) + 0.5d);

请注意,这是使用浮点算术的,而浮点操作中固有的不精确也适用于此处。还要注意,通过将结果值投放为int,该代码可以处理的时间的最大时间差异相当于大约245000年,即给予或接受。我希望那不是问题...

First, the accepted answer is wrong. Lets assume both DateTimePickers contain the same date, with the time of dtpStartTime being 01:00:00 (exactly one o'clock in the morning) and dtpEndTime having a time of 07:00:00 (time for breakfast). Both times also land on whole seconds, so for this example we do not have to be concerned with fractions of a second.

So, what is the time difference between 01:00:00 and 07:00:00 (both are times at the same day)? It is precisely six hours. And yet, the code in the accepted answer produces an incorrect time difference of seven hours, obviously. So, why was that answer accepted, then? I can only guess that the asker prefers getting sometimes incorrect results over getting always correct results.

To give a more meaningful answer, it would first need to be decided how to deal with time differences that are not whole hours, like for example the difference between 01:15:00 and 03:07:47, for example. The question does not give clear directions about this, so i fill the void and define my own rule here: A time difference should be rounded up to the next larger whole hour if the fractional part of the time difference in hours is >= 30 minutes, otherwise the time difference should be rounded down to the next smaller hour. Which basically translates to:

TimeSpan diff = dtpEndTime.Value - dtpStartTime.Value;
int hours = (int) (Math.Abs(diff.TotalHours) + 0.5d);

Note that this is using floating point arithmetic, with the imprecision inherent in floating point operations applying here as well. Also note that by casting the resulting value to int, the maximum time difference in hours this code can handle is equivalent to about 245000 years, give or take. I hope that's not being a problem...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文