DateTimePicker控件(日期时间选择器)《笔记5》

DateTimePicker控件使用笔记

  1. 如何取年限,时间的数据?
    关键词(Value)
 #region Value显示模式
            label5.Text = "年限:" + dateTimePicker4.Value.Year.ToString();
            label6.Text = "月限:" + dateTimePicker4.Value.Month.ToString();
            label7.Text = "日期:" + dateTimePicker4.Value.Date.ToString();
            label8.Text = "当天为几号:" + dateTimePicker4.Value.Day.ToString();
            label9.Text = "星期:" + (int)dateTimePicker4.Value.DayOfWeek;
            label10.Text = "小时:" + dateTimePicker4.Value.Hour.ToString();
            label11.Text = "分钟:" + dateTimePicker4.Value.Minute.ToString();
            label12.Text = "秒钟:" + dateTimePicker4.Value.Second.ToString();
            label13.Text = "当天时间:" + dateTimePicker4.Value.TimeOfDay.ToString();
            label14.Text = "毫秒:" + dateTimePicker4.Value.Millisecond.ToString(); 
            #endregion

DateTimePicker控件(日期时间选择器)《笔记5》_第1张图片

  1. 如何设置DateTimePicker控件的显示格式?
    关键词(Format)
    Long(长日期格式),Short(短日期格式),Time(时间格式),Custom(自定义格式)
            #region 显示长日期和短日期
            dateTimePicker1.Format = DateTimePickerFormat.Long;
            dateTimePicker2.Format = DateTimePickerFormat.Short; 
            #endregion

/**********************************************************/
            #region 显示时间格式
            dateTimePicker3.Format = DateTimePickerFormat.Time;

            //设置控制时间属性ShowUpDown
            dateTimePicker3.ShowUpDown = true; 
            #endregion

/**********************************************************/
            #region 显示自定义格式
            dateTimePicker4.Format = DateTimePickerFormat.Custom;

            //设置自定义显示时间CostomFormat
            string Tim =     CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dateTimePicker4.Value.DayOfWeek);
            dateTimePicker4.CustomFormat = "yyyy年,MM月,dd日" + "  " + Tim;
            //或则
            string TimeValue = dateTimePicker4.value.Tostring("yyyy年MM月dd日");
            #endregion

DateTimePicker控件(日期时间选择器)《笔记5》_第2张图片

  1. 如何把DateTimePicker.Value.DayOfWeek返回的英文名称转换成中文名称?

    1、最直接的一种方法

 System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dateTimePicker4.Value.DayOfWeek); 
//解析:命名空间.日期字符串格式设置.当前线程区域.日期时间格式.返回完整名称(获取当前的时间)

2、用数组的形式

string[] ZhuQi = new string[]{
    "星期日","星期一","星期二","星期三","星期四","星期五","星期六",};
ConSole.WriteLine(ZhuQi[datetimeformat1.value.DayOfWeek]);

3、直接转换

int i = Convert.Toint32(datetimepicker1.value.DayOfWeek);
int j = (int)datetimepicker1.value.DayOfWeek;

学习代码:

    private void DateTimepicker_Load(object sender, EventArgs e)
        {
            #region 显示长日期和短日期
            dateTimePicker1.Format = DateTimePickerFormat.Long;
            dateTimePicker2.Format = DateTimePickerFormat.Short; 
            #endregion

            #region 显示时间格式
            dateTimePicker3.Format = DateTimePickerFormat.Time;
            //设置控制时间属性ShowUpDown
            dateTimePicker3.ShowUpDown = true; 
            #endregion

            #region 显示自定义格式
            dateTimePicker4.Format = DateTimePickerFormat.Custom;
            //设置自定义显示时间CostomFormat
            string Tim = CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dateTimePicker4.Value.DayOfWeek);
            dateTimePicker4.CustomFormat = "yyyy年,MM月,dd日" + "  " + Tim;
            #endregion

            #region 如何把DayOfWeek英文名称转换成中文(三种解决方法)
            //一、最直接的一种
            System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dateTimePicker4.Value.DayOfWeek); 
            //解析:命名空间.日期字符串格式设置.当前线程区域.日期时间格式.返回完整名称(获取当前的时间)

            //二、用数组的形式
            string[] ZhouQi = new string[] {
    "星期日","星期一","星期二","星期三","星期四","星期五","星期六", };
            Console.WriteLine(ZhouQi[Convert.ToInt32(dateTimePicker4.Value.DayOfWeek)]);

            //三、直接转换
            int i = Convert.ToInt32(dateTimePicker4.Value.DayOfWeek);
            int j = (int)dateTimePicker4.Value.DayOfWeek;
            #endregion

            #region Value显示模式
            label1.Text = "设置长日期时间";
            label2.Text = "设置短日期时间";
            label3.Text = "设置只显示时间";
            label4.Text = "设置自定义时间";

            label5.Text = "年限:" + dateTimePicker4.Value.Year.ToString();
            label6.Text = "月限:" + dateTimePicker4.Value.Month.ToString();
            label7.Text = "日期:" + dateTimePicker4.Value.Date.ToString();
            label8.Text = "当天为几号:" + dateTimePicker4.Value.Day.ToString();
            label9.Text = "星期:" + (int)dateTimePicker4.Value.DayOfWeek;
            label10.Text = "小时:" + dateTimePicker4.Value.Hour.ToString();
            label11.Text = "分钟:" + dateTimePicker4.Value.Minute.ToString();
            label12.Text = "秒钟:" + dateTimePicker4.Value.Second.ToString();
            label13.Text = "当天时间:" + dateTimePicker4.Value.TimeOfDay.ToString();
            label14.Text = "毫秒:" + dateTimePicker4.Value.Millisecond.ToString(); 
            #endregion

            toolStripStatusLabel1.Text = DateTime.Now.Year.ToString() +"-"+ DateTime.Now.Month.ToString() +"-"+ DateTime.Now.Day.ToString();
            toolStripStatusLabel3.Text = DateTime.Now.TimeOfDay.ToString();
        }

        private void dateTimePicker4_ValueChanged(object sender, EventArgs e)
        {
            #region 显示自定义格式
            dateTimePicker4.Format = DateTimePickerFormat.Custom;
            //设置自定义显示时间CostomFormat
            string Tim = CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dateTimePicker4.Value.DayOfWeek);
            dateTimePicker4.CustomFormat = "yyyy年,MM月,dd日" + "  " + Tim;
            #endregion
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
            toolStripStatusLabel3.Text = DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString();
        }

DateTimePicker控件(日期时间选择器)《笔记5》_第3张图片

你可能感兴趣的:(Csharp控件解析,控件,日期时间选择器)