如何将 DatePicker 的日期设置为昨天?
这:(
dtpFrom.DisplayDate = DateTime.Now -1;
这是在 Delphi 中完成的),不起作用;我得到,“运算符 '-' 不能应用于类型为 'System.DateTime' 和 'int' 的操作数”
更新:好的,我知道我可以做到这一点,尽管我不知道是否可以这是最简洁的方法:
int dtYear = DateTime.Now.Year;
int dtMonth = DateTime.Now.Month;
int dtDay = DateTime.Now.Day;
dtpFrom.SelectedDate = new DateTime(dtYear, dtMonth, dtDay - 1);
...现在我看到有更好的方法,但在选择正确答案之前必须等待一分钟。
This:
dtpFrom.DisplayDate = DateTime.Now -1;
(which is how it was done in Delphi), does not work; I get, "Operator '-' cannot be applied to operands of type 'System.DateTime' and 'int'"
Update: OK, I see I can do this, although I don't know if this is the most succinct way:
int dtYear = DateTime.Now.Year;
int dtMonth = DateTime.Now.Month;
int dtDay = DateTime.Now.Day;
dtpFrom.SelectedDate = new DateTime(dtYear, dtMonth, dtDay - 1);
...and now I see there is a better way, but must wait a minute before selecting a correct answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果有人来到这里并想让文本默认显示昨天,请使用
DatePickerName**.SelectedDate = DateTime.Today.AddDays(-1);**
If anyone comes here and wanted to make the text to show yesterday default use
DatePickerName**.SelectedDate = DateTime.Today.AddDays(-1);**