2012-12-04 174 views
3

我有一个DateTime选择器到达时间添加到列表中,我对此2个问题:日期时间选择器C#格式

  1. 我怎样才能得到它显示的日期一样12-Jan-2012而不是12/01/12
  2. 我怎样才能让它显示日期后的时间,但不是当前时间,因为这是什么显示atm。

我当前的代码是不是很先进的只是:

theVisit.ArrivalTime = DateTimePicker1.Value 

回答

6

像这样的东西能显示日期和时间:

DateTimePicker1.Value.ToString("d-MMM-yyyy hh:mm:ss"); 

若要覆盖默认的DateTimePicker设置,你可以做这个:

DateTimePicker1.Format = DateTimePickerFormat.Custom; 
DateTimePicker1.CustomFormat = "d-MMM-yyyy hh:mm:ss"; 

你可以显示一个不同的时间通过修改格式字符串,例如:

DateTimePicker1.CustomFormat = "d-MMM-yyyy 12:00:00"; 

甚至

DateTime otherTime = DateTime.Now; 
DateTimePicker1.CustomFormat = "d-MMM-yyyy " + otherTime.ToString("hh:mm:ss"); 
+0

@嘿,我得到的错误不能隐式地将类型字符串转换为system.datetime – TAM

1

你要找的日期时间格式的字符串。他们在MSDN上有一篇很棒的文章。 有两种类型:

  1. Standard DateTime Formats
  2. Custom DateTime Formats

您可以使用这些构建日期时间要看你怎么想它。 对于你的榜样,你可以使用:

DateTimePicker1.Value.ToString("dd-MMM-yyyy hh:mm:ss") 
3

对于它在拾取该格式显示,设置如下

dateTimePicker1.Format = DateTimePickerFormat.Custom; 
dateTimePicker1.CustomFormat = "dd-MMM-yyyy hh:mm:ss"; 
0

第一属性,改变如何你的DateTimePicker显示DateTime是否:

DateTimePicker1.CustomFormat = "d-MMM-yyyy hh:mm:ss"; 
DateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom; 

您的代码:

theVisit.ArrivalTime = DateTimePicker1.Value; 

不会改变,因为DateTimePicker1.Value没有任何区别,只是根据您的格式显示。

如果您希望控件显示一个特定的时间(而不是当前的时间),你必须给那些重视控制:

DateTimePicker1.Value = new DateTime(2012, 12, 21, 23, 59, 59); 

否则,它会显示当前时间,创作形式。