1. 程式人生 > >使用ToDateTime方法轉換日期顯示格式

使用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的格式和值一定要符合要求,不然異常