问题
How can I set the Datetimepicker
-Format to MM/YYYY?
回答1:
Use DateTimerPicker.Format
property. Check MSDN
public void SetMyCustomFormat()
{
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM/yyyy";
}
回答2:
You can use DateTimerPicker.Format
property as following:
DateTimerPicker.Format = DateTimePickerFormat.Custom;
DateTimerPicker.CustomFormat = "MMMM yyyy";
DateTimerPicker.ShowUpDown = true;
Note: DateTimerPicker.ShowUpDown = true;
is for making the calendar not visible
回答3:
This will give you some more options to display date with diff formats.
DateTimerPicker.Format = DateTimePickerFormat.Custom;
DateTimerPicker.CustomFormat = "MM/yyyy"; // this line gives you only the month and year.
DateTimerPicker.ShowUpDown = true;
Below are the different formats to display date alone on the date time picker control of the Windows applciation C#
DateTimerPicker.CustomFormat = "MMMM yyyy";
The above code will display the full month name and year like "August 2018"
DateTimerPicker.CustomFormat = "MMMM yyyy";
The above code will display the full month name, date and year like "08 August 2018"
DateTimerPicker.CustomFormat = "dd MM yyyy";
This line will give the date in the specified format with out forward slash "08 08 2018"
DateTimerPicker.CustomFormat = "dd/MM/yyyy";
This line will give more specific format to the date "08/08/2018"
DateTimerPicker.CustomFormat = "dd/MMM/yyyy";
This line give date in the format of "08/Aug/2018"
回答4:
This line worked for me
DateTimePicker1.Value.ToString("MMMM dd yyyy")
output: August 2019
来源:https://stackoverflow.com/questions/10137022/how-to-set-datetimepicker-to-month-and-year-only-format