c# - 如何在两个日期实例之间查找一个月中给定日期的每月日期
问题描述
我想知道如何通过 datetimepicker 查找两个日期之间的日期数。例如日期范围是从 2019.02.22 到 2019.06.19,我必须找到第 23 个日期(2019.02.23/2019.03.23/2019.04.23/2019.05.23)我希望得到文本框的结果为 4,但是我尝试使用下面的代码并得到结果为 3,这是错误的答案。如果有人可以帮助我,我们将不胜感激。
DateTime d5 = dateTimePicker1.Value;
DateTime d6 = dateTimePicker3.Value;
int MonthsElapsednew = ((d5.AddDays(1).Year - d6.AddDays(1).Year)
* 12) +(d5.AddDays(1).Month - d6.AddDays(1).Month)
(d5.AddDays(1).Day < d6.AddDays(1).Day ? 1 : 0);
textBox16.Text = MonthsElapsednew.ToString();
解决方案
基于如何在 C# 中获取两个日期之间的日期,它就像这样简单:
DateTime startdate = new DateTime(2019, 2, 22);
DateTime enddate = new DateTime(2019, 6, 19);
int monthselapsed = Enumerable
.Range(0, int.MaxValue)
.Select(index => new DateTime?(startdate.AddDays(index)))
.TakeWhile(date => date <= enddate)
.Where(day => day.Value.Day == 23)
.Count();
推荐阅读
- python - 如何在子对象中获取父对象
- deep-learning - RuntimeError: 4 维权重 [1024, 64, 3, 3] 的预期 4 维输入,但输入的大小为 [32, 10]
- java - 尝试在 ODI 中删除 java 代码中的行尾字符
- html - 获取选定的选项卡以切换图标 Ionic 5
- flutter - 如何在 Dart 中创建证书(X509Certificate)?
- r - 下载在 Shiny 中创建的表格
- python - 使用 python3 而不是默认的 python 2.7 运行脚本
- facebook - Facebook 应用的“添加产品”页面中没有可添加的产品
- mysql - 从一个表中选择行,当存在时使用另一个表中的值
- javascript - Jquery查找元素是否包含超过特定文本