C#日期时间条件,如果当天的值不是5和20
我正在使用datagrid创建一些付款计划。我的场景是,当日期时间为MM/4/YYYY、MM/3/YYYY、MM/2/YYYY、MM/1/YYYY等以及MM/21/YYYY、MM/18//YYYY等时,必须在付款计划之前的5和20输出日期 当点击“执行”按钮时,我在这里使用for循环C#日期时间条件,如果当天的值不是5和20,c#,datetime,C#,Datetime,我正在使用datagrid创建一些付款计划。我的场景是,当日期时间为MM/4/YYYY、MM/3/YYYY、MM/2/YYYY、MM/1/YYYY等以及MM/21/YYYY、MM/18//YYYY等时,必须在付款计划之前的5和20输出日期 当点击“执行”按钮时,我在这里使用for循环 private void execute_Click(object sender, EventArgs e){ int count; int loop_number = 8; // Example I pick
private void execute_Click(object sender, EventArgs e){
int count;
int loop_number = 8;
// Example I pick August 5
var fromDate = date_from.Value; // This the datetimepicker on the toolbox
for (count = 0; count < loop_number ; count++) {
// The output will be shown in the datagrid with a name of "dgv_result"
fromDate = fromDate.AddDays(15);
dgv_result.Rows[count].Cells[0].Value = fromDate;
}
}
注意:只需查看日期即可
这是上面截图的循环
var fromDate=日期\从值;
整数计数
for (count = 0; count < Loop_number; count++) {
dgv_result.Rows.Add(1);
...
dgv_result.Rows[count].Cells[0].Value = fromDate;
}
for(count=0;count
像这样吗
int loop_number = 8;
var fromDate = date_from.Value; // This the datetimepicker on the toolbox
for (int count = 0; count < loop_number ; count++)
{
var currentDate = fromDate.AddMonths(count);
if (currentDate.Day <= 5)
{
currentDate = new DateTime(currentDate.Year, currentDate.Month, 5);
}
else
{
currentDate = new DateTime(currentDate.Year, currentDate.Month, 20);
}
dgv_result.Rows[count].Cells[0].Value = currentDate;
}
int-loop_number=8;
var fromDate=date_from.Value;//这是工具箱上的日期时间选择器
for(int count=0;count 如果(currentDate.Day标记,您的问题仍然不完全清楚,但我假设:假设用户选择2015年8月16日,那么您希望打印出8月5日、8月20日、9月5日、9月20日、10月5日、10月20日、11月5日和11月20日
下面的代码应该可以做到这一点。请记住,一天加两次15天并不总是给你下个月的第5天和第20天(除非本月有30天)
//循环减少为4,因为每次迭代都会填充两个值
int-loop_数=4;
//将所选日期标准化为已知的月份第1天
DateTime firstOfMonth=新的日期时间(fromDate.Year,fromDate.Month,1);
用于(计数=0;计数<循环数;计数++)
{
//计算月5日
DateTime schedule1=每月第一次添加天数(4);
dgv_result.Rows[count]。单元格[0]。值=schedule1;
//计算每月20日
DateTime schedule2=每月第一次添加天数(19);
dgv_result.Rows[count]。单元格[0]。值=schedule2;
firstOfMonth=第一个月。添加月份(1);
}
在每次迭代中,您需要在fromDate中添加15
天
试试这个
private void execute_Click(object sender, EventArgs e)
{
int count;
int loop_number = 8;
// Example I pick August 5
var fromDate = date_from.Value; // This the datetimepicker on the toolbox
for (count = 0; count < loop_number ; count++)
{
dgv_result.Rows[count].Cells[0].Value = fromDate.AddDays(15);
}
}
private void execute\u单击(对象发送方,事件参数e)
{
整数计数;
int loop_编号=8;
//我选择的例子是8月5日
var fromDate=date\u from.Value;//这是工具箱上的日期时间选择器
用于(计数=0;计数<循环数;计数++)
{
dgv_result.Rows[count]。单元格[0]。值=fromDate.AddDays(15);
}
}
你在哪里给出4和/或19的条件?你能重新表述你的问题吗?你说的第4天和第19天是什么意思?你是说第4天还是第19天?也就是说,如果是第4天或第19天,那么第4天变为第19天,第19天变为第20天?当然,先生。我会编辑它。我已经编辑完毕。@mark-我想你不需要叫任何人“先生”这里,除非你知道他们被皇室封为爵士。-)不,先生,日期的值是错误的。它以相同的日期旋转8个循环。:(@mark我不清楚你想得到什么。你能举个例子吗?简单的表和输出,先生,我添加了一些我的应用程序的屏幕截图:)先生,我还有一个问题,你看日期,我选择了2015年8月5日,但循环的第一次执行是2015年8月20日,循环的第一次计数必须是2015年8月5日:)知道吗,先生?我为什么会有这个问题吗?先生,请检查我上面的屏幕截图:)谢谢先生,先生,我只需要日期,因为我有不同的日期代码。我是我的例子是不做8循环。
// loops reduced to 4 because two values are filled per iteration
int loop_number = 4;
// normalize chosen date to known day of month, the 1st
DateTime firstOfMonth = new DateTime(fromDate.Year, fromDate.Month, 1);
for (count = 0; count < loop_number; count++)
{
// calculate 5th of month
DateTime schedule1 = firstOfMonth.AddDays(4);
dgv_result.Rows[count].Cells[0].Value = schedule1;
// calculate 20th of month
DateTime schedule2 = firstOfMonth.AddDays(19);
dgv_result.Rows[count].Cells[0].Value = schedule2;
firstOfMonth = firstOfMonth.AddMonths(1);
}
private void execute_Click(object sender, EventArgs e)
{
int count;
int loop_number = 8;
// Example I pick August 5
var fromDate = date_from.Value; // This the datetimepicker on the toolbox
for (count = 0; count < loop_number ; count++)
{
dgv_result.Rows[count].Cells[0].Value = fromDate.AddDays(15);
}
}