Convert timespan to datetime 添加到C#中的datetimepicker
Convert timespan to datetime To add to a datetimepicker in C #
我想在datatimerpicker中显示一个时间跨度数据类型,我必须先转换但我不知道该怎么做
private void button1_Click(object sender, EventArgs e)
{
TimeSpan a = new TimeSpan(12, 00, 00);
TimeSpan b = new TimeSpan(13, 00, 00);
TimeSpan r = b - a;
TimeSpan rr = new TimeSpan(r.Ticks / 2);
MessageBox.Show("Test\n " + rr); ///this is type TimeSpan
dateTimePicker.Value =Convert.ToDateTime(rr);
// error here
// Additional Information: An object of type 'System.TimeSpan'
// can not be converted to type 'System.IConvertible'. "
}
如果您只想在 DateTimePicker
控件中显示时间,只需将 Format
属性 值更改为 Time
,但是,如果您正在寻找要使用的控件它作为计数器,我建议你使用 TextBox
或 Label
或其他东西 :)
This may in no way be an answer to your question - take it as just an insight into what you are trying to ask and then we can maybe create a real question/answer for it.
Date/Time 选择器必须有日期组件。但是,我们可以 'ignore' 日期部分..
由于问题的模糊性,我只能推测...但是这里是...我的 "could be's" 最终将如何表达。 ..
TimeSpan a = new TimeSpan(12, 00, 00); // 12 hours (could be midday)
TimeSpan b = new TimeSpan(13, 00, 00); // 13 hours (could be 1 pm)
TimeSpan r = b - a; // 1 hour (could be 1 am)
TimeSpan rr = new TimeSpan(r.Ticks / 2); // 30 minutes (could be 12:30 am)
dateTimePicker.Value = DateTime.Now.Add(rr); // current date time plus 30 minutes
// -- OR --
dateTimePicker.Value = DateTime.Now.Date.Add(rr); // current date plus 30 minutes
// eg: 2017-08-29 00:30:00
我想这是你要找的最后一个,但加上这个...
dateTimePicker.Format = DateTimePickerFormat.Time;
有关详细信息,请参阅此内容:https://msdn.microsoft.com/en-us/library/ms229631(v=vs.100).aspx
尝试:
private void button1_Click(object sender, EventArgs e){
TimeSpan a = new TimeSpan(12, 00, 00);
TimeSpan b = new TimeSpan(13, 00, 00);
TimeSpan r = b - a;
TimeSpan rr = new TimeSpan(r.Ticks / 2);
MessageBox.Show("Test\n " + rr); ///this is type TimeSpan
dateTimePicker.Value = DateTime.Now.Date.AddMilliseconds(rr.TotalMilliseconds);
}
我想在datatimerpicker中显示一个时间跨度数据类型,我必须先转换但我不知道该怎么做
private void button1_Click(object sender, EventArgs e)
{
TimeSpan a = new TimeSpan(12, 00, 00);
TimeSpan b = new TimeSpan(13, 00, 00);
TimeSpan r = b - a;
TimeSpan rr = new TimeSpan(r.Ticks / 2);
MessageBox.Show("Test\n " + rr); ///this is type TimeSpan
dateTimePicker.Value =Convert.ToDateTime(rr);
// error here
// Additional Information: An object of type 'System.TimeSpan'
// can not be converted to type 'System.IConvertible'. "
}
如果您只想在 DateTimePicker
控件中显示时间,只需将 Format
属性 值更改为 Time
,但是,如果您正在寻找要使用的控件它作为计数器,我建议你使用 TextBox
或 Label
或其他东西 :)
This may in no way be an answer to your question - take it as just an insight into what you are trying to ask and then we can maybe create a real question/answer for it.
Date/Time 选择器必须有日期组件。但是,我们可以 'ignore' 日期部分..
由于问题的模糊性,我只能推测...但是这里是...我的 "could be's" 最终将如何表达。 ..
TimeSpan a = new TimeSpan(12, 00, 00); // 12 hours (could be midday)
TimeSpan b = new TimeSpan(13, 00, 00); // 13 hours (could be 1 pm)
TimeSpan r = b - a; // 1 hour (could be 1 am)
TimeSpan rr = new TimeSpan(r.Ticks / 2); // 30 minutes (could be 12:30 am)
dateTimePicker.Value = DateTime.Now.Add(rr); // current date time plus 30 minutes
// -- OR --
dateTimePicker.Value = DateTime.Now.Date.Add(rr); // current date plus 30 minutes
// eg: 2017-08-29 00:30:00
我想这是你要找的最后一个,但加上这个...
dateTimePicker.Format = DateTimePickerFormat.Time;
有关详细信息,请参阅此内容:https://msdn.microsoft.com/en-us/library/ms229631(v=vs.100).aspx
尝试:
private void button1_Click(object sender, EventArgs e){
TimeSpan a = new TimeSpan(12, 00, 00);
TimeSpan b = new TimeSpan(13, 00, 00);
TimeSpan r = b - a;
TimeSpan rr = new TimeSpan(r.Ticks / 2);
MessageBox.Show("Test\n " + rr); ///this is type TimeSpan
dateTimePicker.Value = DateTime.Now.Date.AddMilliseconds(rr.TotalMilliseconds);
}