如何在文本框中将当前时间与两个给定时间范围进行比较。 C# [英] How can I compare current time with two given time ranges in a text box. C#

查看:92
本文介绍了如何在文本框中将当前时间与两个给定时间范围进行比较。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框,包含医生的时间范围a,如20:00到04:00。我想将这些时间范围与当前时间进行比较,以判断医生是否可用。





[ ^ ]



请注意医生时间从20 00开始到06 00和当前时间21:01:54。 
它在范围内,所以我只会在Doctor Status中打印Available。

我已经使用4个组合框将医生的时间存储在医生信息中,并在收集中添加24小时时间。
有没有办法使它工作或我将不得不重做DateTime格式的时间?如果是,那么请帮助一个示例代码。
谢谢。





我尝试过:



< pre> DateTime time = DateTime.Today; 
DateTime hr1 = new DateTime(time.Year,time.Month,time.Day, 6 0 0 );
DateTime hr2 = new DateTime(time.Year,time.Month,time.Day, 20 0 0 );
Statusbox.Text =(DateTime.Now > hr1&& DateTime.Now < hr2).ToString();





此代码。

解决方案

< blockquote>不要将日期和时间保持为字符串;始终使用DateTime对象,您的比较和计算将很容易。你需要它们作为字符串的唯一时间是显示或打印它们。


理查德关于存储的信息:总是使用DateTime。



请注意,每次医生轮班开始时,您都希望生成班次DateTime值的开始和结束,因为DateTime.Now返回的Date将在午夜更改 - 因此在您的示例中,开始和结束都在不同的日子。



然后你可以直接用DateTime.Now比较那些DateTime值,这应该告诉你医生是否在。


Quote:

注意医生时间从20 00到06 00和当前时间21:01:54。



 DateTime time = DateTime.Today; 
DateTime hr1 = new DateTime(time.Year,time.Month,time.Day, 6 0 0 );
DateTime hr2 = new DateTime(time.Year,time.Month,time.Day, 20 0 0 );
Statusbox.Text =(DateTime.Now > hr1&& DateTime.Now < hr2).ToString();



语句和你的代码是关于不同的东西。

你的代码大约是白班,但声明是关于夜班。

您需要改进代码来处理夜班,因为明天夜班结束


I have two text boxes that contains the time range a of doctor like 20:00 to 04:00. I want to compare these time ranges with the current time to tell if the doctor is available or not.



[^]

Notice the Doctor Timings From 20 00 To 06 00 and the Current time 21:01:54.
It is in the range so I'll just print "Available" in Doctor Status.

I have stored the doctor's timing in string in "Doctor's Info" using 4 combo boxes and adding the 24 Hour time in collections.
Is there a way to make it work or I'll have to redo the timings in DateTime format? If yes then please help with an example code.
Thank you.



What I have tried:

<pre>DateTime time = DateTime.Today;
            DateTime hr1 = new DateTime(time.Year, time.Month, time.Day, 6, 0, 0);
            DateTime hr2 = new DateTime(time.Year, time.Month, time.Day, 20, 0, 0);
            Statusbox.Text = (DateTime.Now > hr1 && DateTime.Now < hr2).ToString();



This code.

解决方案

Do not keep dates and times as strings; always use DateTime objects, and your comparisons and calculations will be easy. The only time you need them as strings is when you display or print them.


Richard is spot on about the storage: always use DateTime.

Note that you will want to generate a start and end of shift DateTime value each time the doctor's shift starts, as the Date returned by DateTime.Now will change at midnight - so in your example the start and end are in different days.

You can then compare those DateTime values directly with DateTime.Now and that should tell you if the doctor is in.


Quote:

Notice the Doctor Timings From 20 00 To 06 00 and the Current time 21:01:54.


DateTime time = DateTime.Today;
DateTime hr1 = new DateTime(time.Year, time.Month, time.Day, 6, 0, 0);
DateTime hr2 = new DateTime(time.Year, time.Month, time.Day, 20, 0, 0);
Statusbox.Text = (DateTime.Now > hr1 && DateTime.Now < hr2).ToString();


the statement and your code are about different things.
Your code is about a day shift, but the statement is about a night shift.
You need to improve your code to handle the night shift because the end of night shift is tomorrow.


这篇关于如何在文本框中将当前时间与两个给定时间范围进行比较。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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