gpt4 book ai didi

c# - 将时间跨度转换为日期时间添加到 C# 中的日期时间选择器

转载 作者:行者123 更新时间:2023-11-30 20:17:13 28 4
gpt4 key购买 nike

我想在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'. "
}

最佳答案

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.

日期/时间选择器必须有一个日期组件。但是,我们可以“忽略”日期部分..

由于问题的模糊性,我只能推测...但这里是...我的“可能是”最终将如何表达...

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

关于c# - 将时间跨度转换为日期时间添加到 C# 中的日期时间选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45940357/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com