简体   繁体   中英

C# DateTimePicker time

I have a project that Asks for input from the user (@startdate and @enddate)

Once they select the date they then have to push a button.
After the button is pressed it will run a stored procedure and return results in the report viewer.

The problem is if I select the same day (For example 5/14/2012).

(@startdate and @enddate are both 5/14/12) it will not display any information becuase it is set to 00:00:00.

I would like datetimepicker2 (@enddate) to have the time stamp 23:59:59.

How can I add 23:59:59 as the time for datetimepicker2.value?

最简单的方法是将时间添加到datetimepicker2值:

DateTime value = datetimepicker2.Value.AddSeconds(86399); //86400 is 24 hours

You can use DateTime.AddDays(1).AddSeconds(-1) in c# - but I don't recommend doing that - it looks like you should be modifying your sproc

I'm assuming you doing a BETWEEN in your SQL procedure?

I usually use

YourDate >= @StartDate AND YourDate < DATEADD(d, 1, @EndDate)

This is because when working with timestamps, you need to select the whole of the day (eg 5/14/2012 00:00:00 to 5/14/2012 23:59:59, so using this syntax works because it includes all the time between the start of the start date and the end of the end date

Does your sproc work when choosing an end date that is different from the start date, ie does it return data for 5/15/2012 if you pick 5/14/2012 -> 5/15/2012??

If not then you may be seeing this issue

The minimun value

'08/21/2011 00:00:00'

The maximun value

'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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM