显示DateTimeFormat所有属性的代码

一段显示所有 DateTimeFormat 属性值的代码,方便察看

 

C#代码:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<object> valueList = new List<object>();

        private string GetLevelSpace(int level)
        {
            string result = string.Empty;
            for (int i = 0; i < level; i++)
            {
                result += "  ";
            }
            return result;
        }

        private void ShowPropertyValue(int level, object obj)
        {
            if (valueList.Contains(obj)) return;
            valueList.Add(obj);

            if (obj == null)
            {
                return;
            }
            Type type = obj.GetType();
            PropertyInfo[] pis = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo pi in pis)
            {
                object value = null;
                try
                {
                    value = pi.GetValue(obj, null);
                }
                catch
                {
                    richTextBox1.AppendText(GetLevelSpace(level) + pi.Name + ": 出错");
                    this.richTextBox1.AppendText("/r/n");
                    continue;
                }
                if (pi.PropertyType.IsValueType || pi.PropertyType == typeof(string))
                {
                    richTextBox1.AppendText(GetLevelSpace(level) + pi.Name + ": " + value);
                    richTextBox1.AppendText("/r/n");
                }
                else if (pi.PropertyType == typeof(string[]))
                {
                    string[] values = (string[])pi.GetValue(obj, null);
                    richTextBox1.AppendText(GetLevelSpace(level) + pi.Name + ": " + values[0]);
                    for (int i = 1; i < values.Length; i++)
                        richTextBox1.AppendText(", " + values[i]);
                    richTextBox1.AppendText("/r/n");
                }
                else
                {
                    richTextBox1.AppendText(GetLevelSpace(level) + pi.Name + ":/r/n");
                    ShowPropertyValue(++level, value);
                }
            }           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text = "";
            valueList.Clear();

            ShowPropertyValue(0, CultureInfo.CurrentCulture.DateTimeFormat);   

            DateTime dt = DateTime.Now;
            richTextBox1.AppendText("/r/n默认日期格式的输出:");
            richTextBox1.AppendText(dt.ToString() + "/r/n");

            CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            Thread.CurrentThread.CurrentCulture = ci;
            if (!CultureInfo.CurrentCulture.DateTimeFormat.IsReadOnly)
            {
                CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy|MM|dd";
                CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern = "H+mm+ss";
                richTextBox1.AppendText("/r/n自定义日期格式的输出:");
                richTextBox1.AppendText(dt.ToString() + "/r/n");
            }
        }
    }
}

 

窗体文件:

namespace WindowsFormsApplication2
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Dock = System.Windows.Forms.DockStyle.Top;
            this.button1.Location = new System.Drawing.Point(0, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(465, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // richTextBox1
            //
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox1.Location = new System.Drawing.Point(0, 23);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(465, 334);
            this.richTextBox1.TabIndex = 2;
            this.richTextBox1.Text = "";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(465, 357);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.RichTextBox richTextBox1;
    }
}

 

我的显示结果:

AMDesignator: 上午
Calendar:
  MinSupportedDateTime: 0001-01-01 0:00:00
  MaxSupportedDateTime: 9999-12-31 23:59:59
  AlgorithmType: SolarCalendar
  CalendarType: Localized
  Eras:
    Length: 1
    LongLength: 1
    Rank: 1
    SyncRoot:
      IsReadOnly: False
      IsFixedSize: True
      IsSynchronized: False
    TwoDigitYearMax: 2029
    IsReadOnly: True
  DateSeparator: -
  FirstDayOfWeek: Sunday
  CalendarWeekRule: FirstDay
  FullDateTimePattern: yyyy'年'M'月'd'日' H:mm:ss
  LongDatePattern: yyyy'年'M'月'd'日'
  LongTimePattern: H:mm:ss
  MonthDayPattern: M'月'd'日'
  PMDesignator: 下午
  RFC1123Pattern: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
  ShortDatePattern: yyyy-MM-dd
  ShortTimePattern: H:mm
  SortableDateTimePattern: yyyy'-'MM'-'dd'T'HH':'mm':'ss
  TimeSeparator: :
  UniversalSortableDateTimePattern: yyyy'-'MM'-'dd HH':'mm':'ss'Z'
  YearMonthPattern: yyyy'年'M'月'
  AbbreviatedDayNames: 日, 一, 二, 三, 四, 五, 六
  ShortestDayNames: 日, 一, 二, 三, 四, 五, 六
  DayNames: 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六
  AbbreviatedMonthNames: 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月,
  MonthNames: 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月,
  IsReadOnly: True
  NativeCalendarName: 公历
  AbbreviatedMonthGenitiveNames: 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月,
  MonthGenitiveNames: 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月,

 

默认日期格式的输出:2010-02-08 14:37:39

自定义日期格式的输出:2010|02|08 14+37+39

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值