简体   繁体   English

每当用户选择日期时,如何在DateTimePicker中设置默认小时?

[英]How can I set a default hour in the DateTimePicker every time the user selects a date?

I have two DateTimePickers, one named FechaEntrada and the other one FechaSalida . 我有两个DateTimePickers,一个叫FechaEntrada ,另一个叫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. 我想设置FechaEntrada.Value"whatever_date_the_user_picked 00:00:00"FechaSalida.Value"whatever_date_the_user_picked 23:59:59"用户选择的日期,每次。

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. 我需要比较dtpickers的两个值,以便可以使用这两个日期之间选择的值填充数据库的DatagridView。

In my windows form, the user CAN'T choose the hour, just the date. 在我的Windows窗体中,用户无法选择小时,只能选择日期。 How can I do this? 我怎样才能做到这一点? It seems simple but I can't find a solution anywhere. 看起来很简单,但我找不到任何解决方案。

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 DateTimePicker.Value返回一个DateTime

Calling DateTime.Date gives the date as at midnight 00:00:00 (it's still a datetime) - https://docs.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netframework-4.8 调用DateTime.Date给出的日期是午夜00:00:00(仍然是日期时间)-https://docs.microsoft.com/zh-cn/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: 调用AddDays(1)AddSeconds(-1)将使日期向前滚动一天,向后滚动一秒钟,因此:

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.. 那就是Salida时间的变化。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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