从datetime.now检测到经过15分钟的时间

detect 15 minutes elapsed time from datetime.now
2021-07-22
  •  译文(汉语)
  •  原文(英语)

我有一个时间选择器,我想知道如何使它检测到时间选择器中选择的时间是否比时钟提前15分钟DateTime.Now.

如果我将时间选择器设置为9:15,并且DateTime.Now将时间选择器设置为9:31,则应该收到一条消息,我只能将时间设置为实际时间的15分钟.

解决过程1

您可以使用TimeSpan ..

假设您的datetimepicker是TimeOnly ..

TimeSpan difftime = DateTimePicker1.Subtract ( now() );

if (difftime.Minutes > 15)
{
    MessageBox.Show("Max 15 Minutes !"); 
}

如果您的datetimepicker包括日期,那么必须确保 .Hours and .Days = 0

速聊1:
TotalMinutes.或者difftime > TimeSpan.FromMinutes(15)
速聊2:
.. thanx
解决过程2

只需使用

// In the timepicker's time-changed event
if (DateTime.Now.CompareTo(timepicker.Value) == 1 && DateTime.Now.Subtract(timepicker.Value).TotalMilliseconds > 15 * 60 * 1000)
{
     // Do stuff!
     MessageBox.Show("You can only set a max of 15 minutes ahead of the current time!");
     // Cancel the event / set the timepicker's value back to the current time / whatever you prefer
     // One option:
     timepicker.Value = DateTime.Now;
}
速聊1:
在您得到答案之前,我在问题中添加了更多文字
速聊2:
我对相关代码进行了一些修改,但是我的回答基本相同.如果执行时遇到任何麻烦,请告诉我!(如果这样做,请具体说出问题所在)
速聊3:
我遇到以下错误:'System.DateTime.Subtract(System.TimeSpan)'的最佳重载方法匹配有一些无效参数,下一个错误是:参数1:无法从'System.DateTime?转换?到"System.TimeSpan"
速聊4:
-您在使用什么叫"时间选择器"?我一直在假设一个DateTimePicker对象,它是我能找到的最接近的对象...
速聊5:
哦,等等,您似乎已使变量以某种方式可为空.可能是.net 4.5事情...呃,.Value.Subtract(我所拥有的timepicker.Value)参数的末尾添加一个-即使这意味着您有一个.Value.Value-如果没有帮助,请您发布您所要的代码使用?(包括您对我写的内容所做的任何更改)

I have a timepicker and I wonder how I can make it detect if the selected time in the timepicker is 15 minutes ahead of the DateTime.Now.

If I set the timepicker to 9:15 and the DateTime.Now is 9:31 I should get a message that I only can set the time 15 minutes ahead the actual time.

Solutions1

You may use TimeSpan ..

Assumed that your datetimepicker is TimeOnly ..

TimeSpan difftime = DateTimePicker1.Subtract ( now() );

if (difftime.Minutes > 15)
{
    MessageBox.Show("Max 15 Minutes !"); 
}

If your datetimepicker including date, so must ensure that .Hours and .Days = 0

Talk1:
TotalMinutes. Or difftime > TimeSpan.FromMinutes(15)
Talk2:
.. thanx
Solutions2

Just use

// In the timepicker's time-changed event
if (DateTime.Now.CompareTo(timepicker.Value) == 1 && DateTime.Now.Subtract(timepicker.Value).TotalMilliseconds > 15 * 60 * 1000)
{
     // Do stuff!
     MessageBox.Show("You can only set a max of 15 minutes ahead of the current time!");
     // Cancel the event / set the timepicker's value back to the current time / whatever you prefer
     // One option:
     timepicker.Value = DateTime.Now;
}
Talk1:
I added some more text to my question before i got your answer
Talk2:
I slightly modified the code for relevance but my answer is mostly the same. Please tell me if you have any trouble implementing it! (If you do, please say specifically what's going wrong)
Talk3:
I am getting to errors that are the following: The best overloaded method match for 'System.DateTime.Subtract(System.TimeSpan)' has some invalid arguments and the next error is: Argument 1: cannot convert from 'System.DateTime?' to 'System.TimeSpan'
Talk4:
- what are you using that you called 'timepicker'? I've been assuming a DateTimePicker object, which is the closest that I can find...
Talk5:
oh wait, you seem to have made your variable nullable somehow. Possibly a .net 4.5 thing... er, add a .Value to the end of the argument to the .Subtract (What I have as timepicker.Value) - even if that means you have a .Value.Value - if that's no help, could you please post the code you're using? (Including any changes you made from what I wrote)
转载于:https://stackoverflow.com/questions/17377735/detect-15-minutes-elapsed-time-from-datetime-now

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入