datepicker不是选择自定义日期格式?

时间:2011-05-05 06:39:21

标签: c# .net winforms

要求以“2010年1月22日”格式显示日期。

我已编码

 calanderFrom.Format = DateTimePickerFormat.Short;
 calanderFrom.CustomFormat = "dd MMM yyyy";

但日期仍以“5/5/2011”格式出现。请指导我如何更改fromat。

感谢

4 个答案:

答案 0 :(得分:12)

替换

calanderFrom.Format = DateTimePickerFormat.Short;

 calanderFrom.Format = DateTimePickerFormat.Custom;

答案 1 :(得分:3)

不要踢自己:

calanderFrom.Format = DateTimePickerFormat.Custom;

使用DateTimePickerFormat.Custom值代替DateTimePickerFormat.Short

答案 2 :(得分:0)

您必须将格式设置为 System.Windows.Forms.DateTimePickerFormat.Custom

将您的代码更改为:

calanderFrom.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
 calanderFrom.CustomFormat = "dd MMM yyyy";

答案 3 :(得分:0)

使用RFC1123 patternString.Format("{0:r}", dt);

Standard Date and Time Format Strings

说明符 DateTimeFormatInfo属性 模式值(适用于en-US文化)

t   ShortTimePattern    h:mm tt
d   ShortDatePattern    M/d/yyyy
T   LongTimePattern     h:mm:ss tt
D   LongDatePattern     dddd, MMMM dd, yyyy
f   (combination of D and t)    dddd, MMMM dd, yyyy h:mm tt
F   FullDateTimePattern     dddd, MMMM dd, yyyy h:mm:ss tt
g   (combination of d and t)    M/d/yyyy h:mm tt
G   (combination of d and T)    M/d/yyyy h:mm:ss tt
m, M    MonthDayPattern     MMMM dd
y, Y    YearMonthPattern    MMMM, yyyy
r, R    RFC1123Pattern  ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
s   SortableDateTi­mePattern    yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
u   UniversalSorta­bleDateTimePat­tern  yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)