使用ToDateTime方法转换日期显示格式

实现效果:

知识运用:

   Convert类的ToDateTime方法:(将字符串转化为DateTime对象)

    public static DateTime ToDateTime(string value)  //value: 字符串对象,日期和时间的字符串表形式

实现代码:

 1         private void button1_Click(object sender, EventArgs e)
 2         {
 3             if (textBox1.Text != string.Empty &&
 4                 textBox2.Text != string.Empty &&
 5                 textBox3.Text != string.Empty)
 6             {
 7                 string s = string.Format("{0}/{1}/{2}", //得到日期字符串
 8                     textBox1.Text, textBox2.Text, textBox3.Text);
 9                 MessageBox.Show("输入的日期为:"+           //显示消息对话框
10                     Convert.ToDateTime(s).ToLongDateString());
11             }
12             else { MessageBox.Show("没有输入完整,请检查"); }
13         }

注意补充:

  value的格式和值一定要符合要求,不然异常  

原文地址:https://www.cnblogs.com/feiyucha/p/9955623.html