C# 将 datetimepicker 的默认格式设置为 dd-MM-yyyy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19466805/
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
Set default format of datetimepicker as dd-MM-yyyy
提问by C Sharper
I have a datetimepicker which on loading of windows form shows me format in 'MM-dd-yyyy',
我有一个日期时间选择器,它在加载 Windows 窗体时以“MM-dd-yyyy”格式显示我的格式,
as follows:
如下:
I would like it in dd-MM-yyyy
.
我想在dd-MM-yyyy
.
I tried the following:
我尝试了以下方法:
set custom format: "dd-MM-yyyy"
设置自定义格式: "dd-MM-yyyy"
But Its not changing.
但它没有改变。
What could be the problem?
可能是什么问题呢?
Please help me.
请帮我。
采纳答案by ElektroStudios
Ensure that control Format property is properly set to use a custom format:
确保正确设置控件格式属性以使用自定义格式:
DateTimePicker1.Format = DateTimePickerFormat.Custom
Then this is how you can set your desired format:
那么这就是您可以设置所需格式的方法:
DateTimePicker1.CustomFormat = "dd-MM-yyyy"
回答by Umut D.
You could easily use:
您可以轻松使用:
label1.Text = dateTimePicker1.Value.Date.ToString("dd/MM/yyyy")
label1.Text = dateTimePicker1.Value.Date.ToString("dd/MM/yyyy")
and if you want to change '/' or '-', just add this:
如果您想更改“/”或“-”,只需添加以下内容:
label1.Text = label1.Text.Replace(".", "-")
label1.Text = label1.Text.Replace(".", "-")
More info about DateTimePicker.CustomFormat Property: Link
有关DateTimePicker.CustomFormat 属性的更多信息:链接
回答by Pabitra Dash
You can set CustomFormat property to "dd-MM-yyyy" in design mode and use dateTimePicker1.Text property to fetch string in "dd/MM/yyyy" format irrespective of display format.
您可以在设计模式下将 CustomFormat 属性设置为“dd-MM-yyyy”,并使用 dateTimePicker1.Text 属性以“dd/MM/yyyy”格式获取字符串,而不管显示格式。
回答by Priyabrata biswal
Try this,
尝试这个,
string Date = datePicker1.SelectedDate.Value.ToString("dd-MMM-yyyy");
It worked for me the output format will be '02-May-2016'
它对我有用,输出格式为“02-May-2016”
回答by Arthur Zennig
Ammending as "optional Answer". If you don't need to programmatically solve the problem, here goes the "visual way" in VS2012.
修改为“可选答案”。如果你不需要以编程方式解决问题,这里采用 VS2012 中的“可视化方式”。
In Visual Studio, you can set custom format directly from the properties Panel:
在 Visual Studio 中,您可以直接从属性面板设置自定义格式:
First Set the "Format" property to: "Custom"; Secondly, set your custom format to: "dd-MM-yyyy";
首先将“格式”属性设置为:“自定义”;其次,将您的自定义格式设置为:“dd-MM-yyyy”;