[英]To get time along with Date from DateTimePicker
我有一个格式很短的datetimepicker。我必须在后面的代码中获取时间和日期。现在12:00:0am插入日期和日期。相反,我需要插入当前时间以及从datetimepicker中选择的日期。任何人都可以救命?
Create a new DateTime
using the date from your input and the time from DateTime.Now
. 创建一个新的DateTime
使用您输入的日期和时间DateTime.Now
。
DateTime GetDateAndCurrentTime(DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
}
The actual DateTime.Value from the picker is a full DateTime object with the current time. 选择器中的实际DateTime.Value是具有当前时间的完整DateTime对象。
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DateTime dt = dateTimePicker1.Value;
// or
string s = string.Concat(dt.ToShortDateString(), DateTime.Now.ToShortTimeString());
}
Full DateTime: 完整的日期时间:
dateTimePicker1.Value;
'08/21/2011 14:42:11' '2011/08/21 14:42:11'
Short DateTime: 短日期时间:
dateTimePicker1.Value.Date;
'08/21/2011 00:00:00' '2011/08/21 00:00:00'
The minimum value 最小值
'08/21/2011 00:00:00' '2011/08/21 00:00:00'
The maximum value 最大值
'08/21/2011 23:59:59' '08 / 21/2011 23:59:59'
Usage: 用法:
var firstDate = dtpFrom.Value.Date; // 0:00:00
var secondDate = dtpTo.Value.Date.AddSeconds(86400 - 1); //23:59:59 - 86400 is 24 hours
var list = Services.GetListForFilter(firstDate, secondDate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.