C# DateTimePicker:选择日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/93472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTimePicker: pick both date and time
提问by Grzenio
Is it possible to use DateTimePicker (Winforms) to pick both date and time (in the dropdown)? How do you change the custom display of the picked value? Also, is it possible to enable the user to type the date/time manually?
是否可以使用 DateTimePicker (Winforms) 来选择日期和时间(在下拉列表中)?如何更改所选值的自定义显示?另外,是否可以让用户手动输入日期/时间?
采纳答案by itsmatt
Set the Format to Custom and then specify the format:
将格式设置为自定义,然后指定格式:
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm:ss";
or however you want to lay it out. You could then type in directly the date/time. If you use MMM, you'll need to use the numeric value for the month for entry, unless you write some code yourself for that (e.g., 5 results in May)
或者你想布置它。然后您可以直接输入日期/时间。如果您使用 MMM,则需要使用月份的数值进行输入,除非您自己为此编写一些代码(例如,5 月份的 5 个结果)
Don't know about the picker for date and time together. Sounds like a custom control to me.
不知道日期和时间的选择器。对我来说听起来像是自定义控件。
回答by Nescio
Unfortunately, this is one of the many misnomers in the framework, or at best a violation of SRP.
不幸的是,这是框架中的众多误称之一,或者充其量是违反了 SRP。
To use the DateTimePicker for times, set the Format property to either Time or Custom (Use Custom if you want to control the format of the time using the CustomFormat property). Then set the ShowUpDown property to true.
要对时间使用 DateTimePicker,请将 Format 属性设置为 Time 或 Custom(如果要使用 CustomFormat 属性控制时间格式,请使用 Custom)。然后将 ShowUpDown 属性设置为 true。
Although a user may set the date and time together manually, they cannot use the GUI to set both.
虽然用户可以手动设置日期和时间,但他们不能使用 GUI 来设置两者。
回答by Craig
回答by Jeffrey L Whitledge
I'm afraid the DateTimePicker control doesn't have the ability to do those things. It's a pretty basic (and frustrating!) control. Your best option may be to find a third-party control that does what you want.
恐怕 DateTimePicker 控件没有能力做这些事情。这是一个非常基本的(令人沮丧的!)控制。您最好的选择可能是找到一个可以满足您要求的第三方控件。
For the option of typing the date and time manually, you could build a custom component with a TextBox/DateTimePicker combination to accomplish this, and it might work reasonably well, if third-party controls are not an option.
对于手动输入日期和时间的选项,您可以构建一个带有 TextBox/DateTimePicker 组合的自定义组件来实现这一点,如果第三方控件不可用,它可能工作得相当好。
回答by Danish
DateTime Picker can be used to pick both date and time that is why it is called 'Date and Time Picker'. You can set the "Format" property to "Custom" and set combination of different format specifiers to represent/pick date/time in different formats in the "Custom Format" property. However if you want to change Date, then the pop-up calendar can be used whereas in case of Time selection (in the same control you are bound to use up/down keys to change values.
日期时间选择器可用于选择日期和时间,这就是它被称为“日期和时间选择器”的原因。您可以在“设置格式”属性设置为“自定义”和不同的格式说明代表/挑选不同格式的日期/时间在“集组合自定义格式”属性。但是,如果您想更改日期,则可以使用弹出日历,而在选择时间的情况下(在同一控件中,您必须使用向上/向下键来更改值。
For example a custom format " ddddd, MMMM dd, yyyy hh:mm:ss tt" will give you a result like this : "Thursday, August 20, 2009 02:55:23 PM".
例如,自定义格式“ ddddd, MMMM dd, yyyy hh:mm:ss tt”会给你这样的结果:“ 2009 年 8 月 20 日星期四 02:55:23 PM”。
You can play around with different combinations for format specifiers to suit your need e.g MMMMwill give "August" whereas MMwill give "Aug"
您可以使用不同的格式说明符组合来满足您的需要,例如MMMM将给出“ August”,而MM将给出“ Aug”
回答by Vectoria
It is best to use two DateTimePickers for the Job One will be the default for the date section and the second DateTimePicker is for the time portion. Format the second DateTimePicker as follows.
最好为作业使用两个 DateTimePicker,第一个是日期部分的默认值,第二个 DateTimePicker 用于时间部分。按如下方式格式化第二个 DateTimePicker。
timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
timePortionDateTimePicker.ShowUpDown = true;
The Two should look like this after you capture them
捕获它们后,两者应如下所示
To get the DateTime from both these controls use the following code
要从这两个控件中获取 DateTime,请使用以下代码
DateTime myDate = datePortionDateTimePicker.Value.Date +
timePortionDateTimePicker.Value.TimeOfDay;
To assign the DateTime to both these controls use the following code
要将 DateTime 分配给这两个控件,请使用以下代码
datePortionDateTimePicker.Value = myDate.Date;
timePortionDateTimePicker.Value = myDate.TimeOfDay;