0

I have two DateTimePickers, one named FechaEntrada and the other one FechaSalida.

I want to set FechaEntrada.Value to "whatever_date_the_user_picked 00:00:00" and FechaSalida.Value to "whatever_date_the_user_picked 23:59:59" every time the user chooses a date.

I need to compare both values from the dtpickers so I can fill a DatagridView from a DataBase with the values selected between these two dates.

In my windows form, the user CAN'T choose the hour, just the date. How can I do this? It seems simple but I can't find a solution anywhere.

slugster
  • 49,403
  • 14
  • 95
  • 145
Aldimir
  • 193
  • 1
  • 2
  • 16
  • Possible duplicate of [set default time in bootstrap-datetimepicker](https://stackoverflow.com/questions/18005455/set-default-time-in-bootstrap-datetimepicker) – Rahul Sharma Apr 30 '19 at 20:09
  • In the `ValueChanged` event: `[DateTimePicker].Value = [DateTimePicker].Value.Date + new TimeSpan(23, 59, 59);` – Jimi Apr 30 '19 at 20:36
  • The CustomFormat could be: `[DateTimePicker].CustomFormat = $"{CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern} HH:mm:ss";` – Jimi Apr 30 '19 at 20:43

1 Answers1

2

Make it so the user sees/chooses only a date:

FechaEntrada.Format = DateTimePickerFormat.Custom;
FechaEntrada.CustomFormat = "yyyy-MM-dd"; //or whatever date you want
FechaSalida.Format = DateTimePickerFormat.Custom;
FechaSalida.CustomFormat = "yyyy-MM-dd"; //or whatever date you want

When you want to get the dates:

FechaEntrada.Value.Date; //"whatever_date_the_user_picked 00:00:00"
FechaSalida.Value.Date.AddDays(1).AddSeconds(-1); //"whatever_date_the_user_picked 23:59:59".

DateTimePicker.Value returns a DateTime

Calling DateTime.Date gives the date as at midnight 00:00:00 (it's still a datetime) - https://learn.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netframework-4.8

Calling AddDays(1) and AddSeconds(-1) will scroll the date forward one day and back one seconds so:

01-Jan-2000 12:34:56
01-Jan-2000 00:00:00 //.Date
02-Jan-2000 00:00:00 //.AddDays(1)
01-Jan-2000 23:59:59 //.AddSeconds(-1)

That's how the time changes for the salida..

enter image description here

enter image description here

enter image description here

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • This makes 100% sense but I dont know why even when I set the value of ```dtpSalida``` to ```dtpSalida.Date.AddDays(1).AddSeconds(-1)``` the real DateTimePicker value still is 00:00:00. https://gyazo.com/70c181ca6aba07f35f21ccf2b9776c96, – Aldimir May 01 '19 at 15:40
  • In your code screenshot you have 2 important points missing: 1) you declare a variable called `value` and initialize it with the value from the date time picker, then you overwrite `value` with something else; **you never actually set the .Value of dtpSalida** make sure your code contains `dtpSalida.Value = ...` and 2) you're missing the **.Date** in `dtpSalida.Value.Date.AddDays(1).AddSeconds(-1)` - you only wrote `dtpSalida.Value.AddDays(1).AddSeconds(-1)`. The **.Date** is critical, because without it every time your picker value is changed it will go one day forward and one minute back – Caius Jard May 01 '19 at 18:00
  • https://gyazo.com/f40be1b40d47916c47eca05c5a753914 and still gives the same value :( – Aldimir May 02 '19 at 14:08
  • works for me - are you sure your event handlers fire? – Caius Jard May 02 '19 at 14:43
  • note, because i wanted to show the date (not necessary) i put HH:mm:ss in my custom format too – Caius Jard May 02 '19 at 14:47
  • Sorry but it's my first year studying C# and I dont really know how to check if my even handlers fire, and I also tried changing the format to HH:mm:ss – Aldimir May 02 '19 at 15:05
  • If I were (in my code screenshot) to click on the light grey area to the left of number 27 and 22 on the left, then a red dot appears in the light grey area; its a breakpoint. when running in debug mode, the code will stop when it reaches this point. if the code never stops, your event handlers arent linked to your valuechanged event – Caius Jard May 02 '19 at 15:16
  • you can also check by showing the forms designer, clicking a datetimepicker and looking in the properties window lightning bolt to check there is something in the valuechanged cell – Caius Jard May 02 '19 at 15:28
  • i made an example project that works as per my post. you can get it from https://gofile.io/?c=xRysPb – Caius Jard May 02 '19 at 15:35
  • Oh you meant the breakpoints! Yes I already know how to use them, but thanks for your effort tho! – Aldimir May 02 '19 at 16:52