日期时间选择器 C# 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13711358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:31:54  来源:igfitidea点击:

DateTime picker C# format

c#datetimewpftoolkit

提问by TAM

I have a DateTimepicker to add arrival time to a list, I have 2 questions about it:

我有一个DateTime选择器可以将到达时间添加到列表中,我有两个问题:

  1. How can I get it to show dates like 12-Jan-2012Instead of 12/01/12?
  2. How can I get it to show the time after the date but not the current time, as thats what is shows atm.
  1. 我怎样才能让它显示像12-Jan-2012而不是这样的日期12/01/12
  2. 我怎样才能让它显示日期之后的时间而不是当前时间,因为这就是显示 atm 的内容。

My current code is not very advanced its just:

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

theVisit.ArrivalTime = DateTimePicker1.Value

回答by Peter Gluck

Something like this will display the date and time:

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

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

To override the default DateTimePicker settings, you can do this:

要覆盖默认的 DateTimePicker 设置,您可以执行以下操作:

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

You can show a different time by modifying the format string, e.g.:

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

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

or even

甚至

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

回答by Jason Higgins

You're looking for DateTime Format strings. There's a great article on them on MSDN. There's two types:

您正在寻找日期时间格式字符串。MSDN 上有一篇关于它们的很棒的文章。有两种类型:

  1. Standard DateTime Formats
  2. Custom DateTime Formats
  1. 标准日期时间格式
  2. 自定义日期时间格式

You can use these to construct the datetime to look how you want it to. For your example, you would use :

您可以使用这些来构建日期时间以查看您想要的样子。对于您的示例,您将使用:

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

回答by sa_ddam213

For it to show in that format in the picker, set the properties below

要在选择器中以该格式显示,请设置以下属性

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

回答by JYelton

First, to alter how your DateTimePicker displays DateTimes:

首先,要改变 DateTimePicker 显示日期时间的方式:

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

Your code:

您的代码:

theVisit.ArrivalTime = DateTimePicker1.Value;

Would not change, because the DateTimePicker1.Valueisn't any different, it's just displayed according to your format.

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

If you want the control to display a specific time (not the current time), you have to give the control that value:

如果您希望控件显示特定时间(而不是当前时间),则必须为控件提供该值:

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

Otherwise it will display the current time as of the form creation.

否则,它将显示表单创建时的当前时间。

回答by Jpabs

You may check datetimepickerlike this to check if it is empty.

您可以这样检查datetimepicker以检查它是否为空。

if(DatTimePicker1.Text.Equals(" "))
{
     MessageBox.Show("DateTimePicker is empty");
}