views:

67

answers:

4

I’m currently trying to use this control to provide a means of selecting a date and time from a dialog box. However, I can’t seem to work out how to allow selection of a date AND time (format seems to allow short, long, custom and time). Please could someone point me to the relevant property for this?

A: 

I think to solve your problem, you habe to use two DateTimePicker. One for the date and one for the time.

One little example from the msdn

dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
dateTimePicker1.Format = DateTimePickerFormat.Custom;

Or you use one dtp and do this

this.dateTimePicker1.CustomFormat = "dd.MM.yyyy HH:mm:ss";
        this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;

and setting up dtp

this.dateTimePicker1.ShowUpDown = true;

so you have only one dtp.

Sebastian
+1  A: 

Set the picker format to Custom, and then set the CustomFormat property to fit what you want to use.

I'm not sure about whether you can pick times by default, but if you set the ShowUpDown property to True, you should be able to pick pretty much anything using the spin boxes. That probably will disable the calendar, though.

cHao
A: 

Assuming you're using Windows Forms, that doesn't seem to be possible. You could of course set the CustomFormat property to include time, but you can't select both date and time via the GUI.

sm
+2  A: 

If you set a custom format which includes the time, then it can all be edited. For example:

using System;
using System.Windows.Forms;

class Test
{
    [STAThread]
    static void Main()
    {
        Form form = new Form {
            Controls = { 
                new DateTimePicker {
                    Format = DateTimePickerFormat.Custom,
                    CustomFormat = "dd/MM/yyyy HH:mm:ss"
                }
            }
        };
        Application.Run(form);
    }
}

Note that the time part has to be entered using the keyboard in this case - clicking on the dropdown displays a calendar allowing the date to be set, but I don't believe there's an equivalent for the time.

EDIT: Note that if you use ShowUpDown = true this does indeed let you do everything with the mouse - but it means you lose the ability to pick the date in a simple visual way. I think as a user, I'd prefer to just do the time bits with a keyboard.

Jon Skeet