C# 如何仅将 dateTimePicker 中的日期放入变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1092948/
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
How can i put just the date from a dateTimePicker into a variable?
提问by JimDel
I'm using C# and WinForms to try and put just the date from a dateTimePicker into a variable. But I keep getting the date and the time. In the example below textBox1 shows what I'm looking for. But I can't figure out how to do the same for textBox2. Where it gives the date and time. This isn't the exact code I'm using in my app, but a simplified example of it for use as an example. How can I modify it to do what I need? Thanks.
我正在使用 C# 和 WinForms 尝试将 dateTimePicker 中的日期放入变量中。但我一直在获取日期和时间。在下面的示例中 textBox1 显示了我正在寻找的内容。但我不知道如何对 textBox2 做同样的事情。它在哪里给出日期和时间。这不是我在我的应用程序中使用的确切代码,而是用作示例的简化示例。我怎样才能修改它来做我需要的?谢谢。
private void dateTimePicker1_CloseUp(object sender, EventArgs e)
{
DateTime varDate;
textBox1.Text = dateTimePicker1.Value.ToShortDateString();
varDate = dateTimePicker1.Value;
textBox2.Text = Convert.ToString(varDate);
}
采纳答案by Thomas Levesque
varDate = dateTimePicker1.Value.Date;
This will return the DateTimePicker's value with the time set to 00:00:00
这将返回 DateTimePicker 的值,时间设置为 00:00:00
回答by Cody C
Try this
尝试这个
textBox2.Text = varDate.ToShortDateString();
or
或者
varDate = DateTimePicker1.Value.Date;
textBox2.Text = varDate.ToShortDateString() // varDate.ToString("MM/dd/yy") this would also work
回答by Sani Singh Huttunen
Like this:
像这样:
private void dateTimePicker1_CloseUp(object sender, EventArgs e)
{
DateTime varDate;
textBox1.Text = dateTimePicker1.Value.ToShortDateString();
varDate = dateTimePicker1.Value;
textBox2.Text = varDate.ToShortDateString();
}
回答by Phill
Try this :
尝试这个 :
textBox1.Text = dateTimePicker1.Value.ToString("yyyy-MM-dd");
textBox1.Text = dateTimePicker1.Value.ToString("yyyy-MM-dd");
回答by Martin Harris
Thanks to Lazarus' comment above I think I understand your question now...
感谢 Lazarus 上面的评论,我想我现在明白你的问题了......
There is no way for a DateTime variable to only hold a Date value, the best you can do is set the time part to 00:00:00. Unfortunately this will not stop it being printed out when you call the default ToString() method. Therefore the method to solve your issue will entirely depend on what the actual problem you are trying to solve is:
DateTime 变量无法仅保存日期值,您能做的最好的事情是将时间部分设置为 00:00:00。不幸的是,当您调用默认的 ToString() 方法时,这不会阻止它被打印出来。因此,解决您的问题的方法将完全取决于您要解决的实际问题是什么:
If you are trying to display only the Date part in a TextBox then you can use the ToShortDateString() method, or any other date formatting string, but I guess you already know this, since you are using that method with TextBox1.
如果您试图在 TextBox 中仅显示日期部分,那么您可以使用 ToShortDateString() 方法或任何其他日期格式字符串,但我想您已经知道这一点,因为您在 TextBox1 中使用了该方法。
If you are trying to compare two datetimes and don't want the time part to be relevant you can use the Date property to ensure that the time part is set to midnight.
如果您尝试比较两个日期时间并且不希望时间部分相关,您可以使用 Date 属性来确保时间部分设置为午夜。
datetimeA.Date == dateTimeB.Date
Note that this doesn't avoid the comparison of the time part (the objects being compared are still Datetimes, but it does ensure that they are equal.
请注意,这并不能避免时间部分的比较(被比较的对象仍然是 Date times,但它确实确保它们相等。
The only other reason that I can think you may wish to do this is because you feel that the object is too complicated and contains extraneous information. While I very much doubt this is the issue it may comfort you to know that the internals of a DateTime object are little more than a single long value, and you are not wasting space with the unused time portion.
我认为您可能希望这样做的唯一其他原因是因为您觉得对象太复杂并且包含无关信息。虽然我非常怀疑这是一个问题,但知道 DateTime 对象的内部结构只是一个 long 值,并且您没有在未使用的时间部分上浪费空间,这可能会让您感到安慰。