C#DateTimePicker设置自定义格式

摘自Microsoft TechNet DateTimePicker.CustomFormat 属性

 

包含日期和时间分隔符的显示字符串文字或格式字符串,必须在子字符串中使用转义符。 例如,若要显示将日期设置为"6 月 15 日下午 12:00",设置 属性设置为"MMMM dd at t:mm tt"。 如果"at"的子字符串不由转义字符括起来的则结果是"6 月 15 日 aP 12:00 PM"因为字符"t"作为一个字母 a.m./p.m.读取。 格式字符串 (请参阅下面的格式字符串表)。

可结合使用的格式字符串设置格式的日期和时间。 例如,若要显示的日期和时间作为 06/15/2001年 12:00 PM,此属性应设置为"MM / dd / yyyy hh': ' mm tt"。 有关详细信息,请参阅 自定义日期和时间格式字符串

System_CAPS_note注意

属性必须设置为 DateTimePickerFormatCustom ,此属性才会影响显示的日期和时间的格式。

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

格式字符串

描述

d

一个或两位数的日期。

dd

两位数的日期。 位数字的日期值的前面带有为 0。

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")。

tt

两个字母 a.m./p.m.。 缩写 (上午 将显示如"是")。

y

一位数字的年份 (2001年显示为"1")。

yy

年 (2001年显示为"01") 中的最后两位数字。

yyyy

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

下面的代码示例设置 属性,以便 DateTimePicker 将日期显示为"年 6 月 1 日,2001年-Friday"。 此代码假定的一个实例 DateTimePicker 上已创建控件 Form

public void SetMyCustomFormat() 

   // Set the Format type and the CustomFormat string. 
   dateTimePicker1.Format = DateTimePickerFormat.Custom; 
   dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd"; 

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"; 
   }
posted @ 2016-01-14 18:07  JillWen  阅读(2095)  评论(0编辑  收藏  举报