【C#】datetimepicker里面如何设置日期为当天日期,而时间设为0:00或23:59?

今天无意中发现要根据日期查询时间,datatimepicker控件会把时间默认成当前时间(当你的控件只显示日期时),这样查询出来的出来的数据会有误差,用来下面的办法成功设置日期为当天日期,而时间设为0:00或23:59。

1

2

3

4

5

6

//日期起时间 2014-04-04 00:00

DateTime dStart = this.dtp_startDate.Value.Date;

string startDate = dStart.ToString("yyyy-MM-dd HH:mm:ss");//转成字符串

//日期结束时间 2014-04-04 23:59:59

DateTime dEnd = new DateTime(this.dtp_endDate.Value.Year,this.dtp_endDate.Value.Month, this.dtp_endDate.Value.Day, 23, 59, 59);

string endDate = dEnd.ToString("yyyy-MM-dd HH:mm:ss");//转成字符串               

  其中dtp_startDate、dtp_endDate是datetimepicker控件名。

另外,设置00:00还有一中办法:

datetimepicker的value属性只设置日期:2014/04/04(时间不设置),在FormatString属性设置yyyy-MM-dd HH:mm

这样就OK了。

猜你喜欢

转载自blog.csdn.net/Sayesan/article/details/83305435