日期时间选择器,如果条件 [英] Date time picker if condition

查看:90
本文介绍了日期时间选择器,如果条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int currentmonth = DateTime.Now.Month;
            int x = 0;

            if (currentmonth == DateTime.Now.Month)
            {
                x++;
            }
            else
                x = 0;





这是我的代码。此代码是一个按钮的一部分,它将x的值添加到数据库中的某个数据中,当且仅当月份发生变化时(例如当前月份= 3月然后下一次按下该按钮将在5月份)的值x返回0.我不知道如何测试这个,以便我可以看到它是否重置。如果代码片段正确,我想问你的意见。



我尝试了什么:



我试图在我的sql中加入它,到目前为止它的工作原理。但是,如果月份发生变化,我只能真正知道这个片段是否正确。



This is my code. This code is part of a button that adds the value of x to a certain data in the database and if and only if the month changes(ex. current month = March then the next press of the button will be at May) the value of x returns to 0. I don't know how to test this so that I can see if it resets or not. I would like to ask your opinion if the code snippet is correct.

What I have tried:

I've tried to incorporate this in my sql and so far it works. But I can only really know if this snippet is correct if the month changes.

推荐答案

嗯。

所以你的代码获得当前月份,并将它与...当前月份进行比较。

您希望它在月份变化时发现吗?

嗯,它会。但是,只有当月份实际上在DateTime.Now属性的两次访问之间以毫秒(甚至纳秒)变化时 - 这在极端情况下是不太可能的。

可能你真正想做的是创建一个私有DateTime变量并将其预设为初始日期:

Um.
So your code gets the current month, and compares it to...the current month.
And you expect it to spot when the month changes?
Well, it will. But only if the month actually changes in the milliseconds (or even nanoseconds) between the two accesses to the DateTime.Now property - which is unlikely in the extreme.
Probably, what you actually want to do is create a private DateTime variable and preset it to the "initial" date:
private DateTime startedAt = DateTime.Now;

然后在你的按钮中单击处理程序只需检查它:

Then in your button Click handler just check it:

DateTime now = DateTime.Now;
if (startedAt.Month == now.Month)
   {
   x++;
   }
else
   {
   x = 0;
   startedAt = now;
   }


这篇关于日期时间选择器,如果条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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