DateTimePicker.CustomFormat 属性

获取或设置自定义日期/时间格式字符串。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<LocalizableAttribute(True)> _
Public Property CustomFormat As String
用法
Dim instance As DateTimePicker
Dim value As String

value = instance.CustomFormat

instance.CustomFormat = value
[LocalizableAttribute(true)] 
public string CustomFormat { get; set; }
[LocalizableAttribute(true)] 
public:
property String^ CustomFormat {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_CustomFormat ()

/** @property */
public void set_CustomFormat (String value)
public function get CustomFormat () : String

public function set CustomFormat (value : String)

属性值

表示自定义日期/时间格式的字符串。默认为 空引用(在 Visual Basic 中为 Nothing)。

备注

若要显示包含日期和时间分隔符的字符串或格式字符串,则必须在子字符串中使用转义符。例如,若要将日期显示为“June 15 at 12:00 PM”,请将 CustomFormat 属性设置为“MMMM dd 'at' t:mm tt”。如果转义符中不包括“at”子字符串,则结果是“June 15 aP 12:00PM”,因为字符“t”是作为单字母 A.M./P.M. 格式字符串读取的(请参见下面的格式字符串表)。

可组合格式字符串,以设置日期和时间格式。例如,若要将日期和时间显示为 06/15/2001 12:00 PM,应将此属性设置为“MM'/'dd'/'yyyy hh':'mm tt”。有关更多信息,请参见 日期与时间格式字符串

提示

Format 属性必须设置为 DateTimePickerFormat.Custom,此属性才能影响显示的日期和时间的格式设置。

下表列出所有的有效格式字符串及其说明。

格式字符串

说明

d

一位数或两位数的天数。

dd

两位数的天数。一位数天数的前面加一个零。

ddd

三个字符的星期几缩写。

dddd

完整的星期几名称。

h

12 小时格式的一位数或两位数小时数。

hh

12 小时格式的两位数小时数。一位数数值前面加一个 0。

H

24 小时格式的一位数或两位数小时数。

HH

24 小时格式的两位数小时数。一位数数值前面加一个 0。

m

一位数或两位数分钟值。

mm

两位数分钟值。一位数数值前面加一个 0。

M

一位数或两位数月份值。

MM

两位数月份值。一位数数值前面加一个 0。

MMM

三个字符的月份缩写。

MMMM

完整的月份名。

s

一位数或两位数秒数。

ss

两位数秒数。一位数数值前面加一个 0。

t

单字母 A.M./P.M. 缩写(A.M. 将显示为“A”)。

tt

两字母 A.M./P.M. 缩写(A.M. 将显示为“AM”)。

y

一位数的年份(2001 显示为“1”)。

yy

年份的最后两位数(2001 显示为“01”)。

yyyy

完整的年份(2001 显示为“2001”)。

示例

下面的代码示例设置 CustomFormat 属性,使 DateTimePicker 将日期显示为“June 01, 2001 - Friday”(2001 年 6 月 1 日,星期五)。此代码假定已在 Form 上创建 DateTimePicker 控件的实例。

Public Sub SetMyCustomFormat()
    ' Set the Format type and the CustomFormat string.
    dateTimePicker1.Format = DateTimePickerFormat.Custom
    dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd"
End Sub 'SetMyCustomFormat
public void SetMyCustomFormat()
{
   // Set the Format type and the CustomFormat string.
   dateTimePicker1.Format = DateTimePickerFormat.Custom;
   dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
}
public:
   void SetMyCustomFormat()
   {
      // Set the Format type and the CustomFormat string.
      dateTimePicker1->Format = DateTimePickerFormat::Custom;
      dateTimePicker1->CustomFormat = "MMMM dd, yyyy - dddd";
   }
public void SetMyCustomFormat()
{
    // Set the Format type and the CustomFormat string.
    dateTimePicker1.set_Format(DateTimePickerFormat.Custom);
    dateTimePicker1.set_CustomFormat("MMMM dd, yyyy - dddd");
} //SetMyCustomFormat

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

DateTimePicker 类
DateTimePicker 成员
System.Windows.Forms 命名空间
DateTimePickerFormat
Format