- 將DateTimePicker的值(日期時間)轉為字串 :
string myDate = myDateTimePicker.Value.ToString("yyyyMMdd");
string myDateTime = myDateTimePicker.Value.ToString("yyyyMMddHHmmss");
- 將系統時間轉字串
string myDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
- 將字串轉為DateTimePicker的值(日期時間):
myDateTimePicker.Value = DateTime.Parse(myDateTimeString);
- DateTimePicker如何顯示特定的日期格式:
myDateTimePicker.CustomFormat = "yyyy-MM-dd HH:mm:ss";
myDateTimePicker.Value = DateTime.Parse(myDateTimeString);
- 如果沒有時間,DateTimePicker如何顯示空白?
myDateTimePicker.CustomFormat = " ";
myDateTimePicker.Format = DateTimePickerFormat.Custom;
- 一個範例程式:
//the myDateTimeString is a string. //the myDateTimePicker is a DateTimePicker control if (myDateTimeString.Length == 14) { //if myDateTimeString is not blank; ex, myDateTimeString = "20190125153050" myDateTimeString = myDateTimeString.Substring(0, 4) + "-" + myDateTimeString.Substring(4, 2) + "-" + myDateTimeString.Substring(6, 2) + " " + myDateTimeString.Substring(8, 2) + ":" + myDateTimeString.Substring(10, 2) + ":" + myDateTimeString.Substring(12, 2); myDateTimePicker.CustomFormat = "yyyy-MM-dd HH:mm:ss"; myDateTimePicker.Value = DateTime.Parse(myDateTimeString); } else { //if myDateTimeString is empty, make the column show blank. myDateTimePicker.CustomFormat = " "; myDateTimePicker.Format = DateTimePickerFormat.Custom; }
沒有留言:
張貼留言