datetimepicker--Date Control
Purpose: Use to select a date and time, but only one time, not a continuous period. Of course, you can also enter the date and time directly
The Format property of the DateTimePicker is set to time, so that only times are displayed in the time control.
The Format property is used to get or set the date and time formats displayed in the control
The DateTimePickerFormat enumeration values are as follows:
Custom DateTimePicker control displays date/time values in a customized format
The long DateTimePicker control is displayed in a longer date format set by the user's operating system
short DateTimePicker controls are displayed in a shorter date format set by the user's operating system
the time DateTimePicker control is displayed in the format of the user's operating system settings
Instance code:
private void Form1_Load (object sender, EventArgs e) { //Set the Format property of the DateTimePicker1 to time so that it displays only the Times <span style= "COLOR: #FF0000;" >datetimepicker1.format = datetimepickerformat.time;</span> TextBox1.Text = dateTimePicker1.Text;// Use a text box to get the time the control is displayed }
Run:
So how do we customize control display date ...
Of course we need to use the custom enumeration value of the DateTimePickerFormat mentioned above.
Also use the CustomFormat property of the DateTimePicker
Valid date format strings and descriptions:
D single-digit or double-digit number of days
DD Two-digit number of days, one number of days before plus one 0
DDD 3-character weekday abbreviation (Monday)
dddd name of the full day of the week (Monday)
H 12-hour format for one-or two-digit hours
HH two-digit hours in 12-hour format, preceded by a 0-digit value
H 24-hour format for one-or two-digit hours
HH two-digit hours in 24-hour format, preceded by a 0-digit value
M single-digit or two-digit minute value
MM Two-digit minute value, preceded by a 0 digit value
M one-digit or two-digit month value
MM Two-digit month value, preceded by a 0 digit value
MMM 3-Character month abbreviation
MMMM Full Month value
s one-digit or two-digit number of seconds
SS two-digit number of seconds, preceded by a 0 digit value
T single-letter a.m./p.m abbreviation (a.m. will be shown as "A")
TT two letter a.m./p.m abbreviation (a.m. will be displayed as "AM")
Y one-digit year (2001 shown as "1")
Last two digits of YY year (2001 shown as "01")
YYYY full year (2001 shown as "2001")
Instance code:
private void Form1_Load (object sender, EventArgs e) { <span style= "color: #FF0000;" >//set the Format property to custom, which makes the time format of the user's self-calibration effective Datetimepicker1.format = Datetimepickerformat.custom; The custom format is set by the CustomFormat property of the control Datetimepicker1.customformat = "MMMM dd, yyyy-dddd";</span> Label1. Text = Datetimepicker1.text; }
Run Time:
How to obtain a single date of the year, month, day and other information.
We can get the Year,month and day properties of the Value property of the DateTimePicker control by using the
Instance code:
private void Form1_Load (object sender, EventArgs e) { //Use the control's Text property to get the date selected by the current control TextBox1.Text = Datetimepicker1.text; Use the year method of the Value property to get the annual TextBox2.Text = dateTimePicker1.Value.Year.ToString () of the selected date; The year method using the Value property gets the month of the selected date TextBox3.Text = DateTimePicker1.Value.Month.ToString (); The year method using the Value property gets the day Textbox4.text = dateTimePicker1.Value.Day.ToString () of the selected date;
Run: