我正在尝试将datetimepicker设置为今天的日期和选定的日期。然后将第二个选择器设置为明天日期和选定时间。我的问题是,虽然我称这种方法,但它不会增加日子。如何更改此项以添加一天并保持所需的时间?
如果我删除转换我得到一个错误:System.DateTime.DateTime(int, int, int, int, int, int)
的最佳重载方法匹配有一些无效的参数
RadDateTimePicker1.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 30, 0);
RadDateTimePicker2.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, Convert.ToInt32(DateTime.Now.AddDays(1)), 5, 30, 0);
答案 0 :(得分:3)
我想你想要DateTime.Now.AddDays(1).Day
new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.AddDays(1).Day, 5, 30, 0);
AddDays
方法返回新的DateTime
,您无法将其转换为int
。只需使用Day
属性即可获取新日期时间。
更新: @UlugbekUmirov关于他的评论是正确的,为了解决这个问题,你可以尝试:
var now = DateTime.Now;
DateTime first = new DateTime(now.Year,now.Month,now.Day, 17, 30, 0);
DateTime second;
if (now.Day == DateTime.DaysInMonth(now.Year, now.Month))
{
if(DateTime.Now.Month == 12)
second = new DateTime(now.Year+1, 1, 1, 5, 30, 0);
else
second = new DateTime(now.Year, now.Month+1, 1, 5, 30, 0);
}
else
{
second = new DateTime(now.Year, now.Month, now.Day+1, 5, 30, 0);
}
RadDateTimePicker1.SelectedDate = first;
RadDateTimePicker2.SelectedDate = second;
答案 1 :(得分:2)
尝试类似
的内容DateTime today = DateTime.Now.Date + new TimeSpan( 0 , 17 , 30 , 0 ) ;
DateTime tomorrow = today.Date + new TimeSpan( 1 , 5 , 30 , 0 ) ;
RadDateTimePicker1.SelectedDate = today ;
RadDateTimePicker2.SelectedDate = tomorrow ;
答案 2 :(得分:1)
DateTime.Now
可以在通话之间切换,最好执行以下操作:
DateTime today = DateTime.Today;
RadDateTimePicker1.SelectedDate = today.Add(new TimeSpan(17, 30, 0));
RadDateTimePicker2.SelectedDate = today.Add(new TimeSpan(1, 5, 30, 0));
答案 3 :(得分:1)
接受的答案非常详细(问题中的示例代码也是如此)。
为什么不这样:
RadDateTimePicker1.SelectedDate = DateTime.Today.AddHours(17.5);
RadDateTimePicker2.SelectedDate = DateTime.Today.AddHours(29.5);
或者,如果你想更清楚第二行的时间成分,你可以改用它:
RadDateTimePicker2.SelectedDate = DateTime.Today.AddDays(1).AddHours(5.5);