Set default format of datetimepicker as dd-MM-yyyy

左心房为你撑大大i 提交于 2019-12-03 09:08:49

问题


I have a datetimepicker which on loading of windows form shows me format in 'MM-dd-yyyy',

as follows:

I would like it in dd-MM-yyyy.

I tried the following:

set custom format: "dd-MM-yyyy"

But Its not changing.

What could be the problem?

Please help me.


回答1:


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"



回答2:


Ammending as "optional Answer". If you don't need to programmatically solve the problem, here goes the "visual way" in VS2012.

In Visual Studio, you can set custom format directly from the properties Panel:

First Set the "Format" property to: "Custom"; Secondly, set your custom format to: "dd-MM-yyyy";




回答3:


You could easily use:

label1.Text = dateTimePicker1.Value.Date.ToString("dd/MM/yyyy")

and if you want to change '/' or '-', just add this:

label1.Text = label1.Text.Replace(".", "-")

More info about DateTimePicker.CustomFormat Property: Link




回答4:


Try this,

string Date = datePicker1.SelectedDate.Value.ToString("dd-MMM-yyyy");

It worked for me the output format will be '02-May-2016'




回答5:


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.



来源:https://stackoverflow.com/questions/19466805/set-default-format-of-datetimepicker-as-dd-mm-yyyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!