C# 在 DateTime 对象中存储短日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1591836/
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
Storing a short date in a DateTime object
提问by contactmatt
I'm trying to store a shortened date (mm/dd/yyyy) into a DateTime object. The following code below is what I am currently trying to do; this includes the time (12:00:00 AM) which I do not want :(
我正在尝试将缩短的日期 (mm/dd/yyyy) 存储到 DateTime 对象中。下面的代码是我目前正在尝试做的;这包括我不想要的时间 (12:00:00 AM) :(
DateTime goodDateHolder = Convert.ToDateTime(DateTime.Now.ToShortDateString());
Result will be 10/19/2009 12:00:00 AM
结果将是 10/19/2009 12:00:00 AM
采纳答案by Dinah
DateTime
is an integer interpreted to represent both parts of DateTime (ie: date and time). You will always have both date and time in DateTime
. Sorry, there's nothing you can do about it.
DateTime
是一个整数,解释为代表 DateTime 的两个部分(即:日期和时间)。您将始终拥有日期和时间DateTime
。抱歉,您对此无能为力。
You can use .Date to get the date part. In these cases, the time will always be 12:00 but you can just ignore that part if you don't want it.
您可以使用 .Date 来获取日期部分。在这些情况下,时间将始终为 12:00,但如果您不想要它,则可以忽略该部分。
回答by Petros
Instead of .Now
you can use .Today
which will not remove the time part, but will only fill the date part and leave time to the default value.
而不是.Now
您可以使用.Today
which 不会删除时间部分,而只会填充日期部分并将时间保留为默认值。
Later on, as others pointed out, you should try to get the date part ignoring the time part, depending on the situation.
后来,正如其他人指出的那样,您应该根据情况尝试获取日期部分而忽略时间部分。
回答by ChaosPandion
You only have two options in this situation.
在这种情况下,您只有两种选择。
1) Ignore the time part of the value.
1) 忽略值的时间部分。
2) Create a wrapper class.
2)创建一个包装类。
Personally, I am inclined to use option 1.
就个人而言,我倾向于使用选项 1。
回答by TLiebe
A DateTime will always have a time component - even if it is 12:00:00 AM. You just need to format the DateTime when you display it (e.g. goodDateHolder.ToShortDateString()).
DateTime 将始终具有时间组件 - 即使它是 12:00:00 AM。您只需要在显示 DateTime 时对其进行格式化(例如 goodDateHolder.ToShortDateString())。
回答by nabrond
DateTime object stores both the date and the time. To display only the date, you would use the DateTime.ToString(string) method.
DateTime 对象存储日期和时间。要仅显示日期,您可以使用 DateTime.ToString(string) 方法。
DateTime goodDateHolder = DateTime.Now;
// outputs 10/19/2009
Console.WriteLine(goodDateHolder.ToString("MM/dd/yyyy"));
For more information on the ToString method, follow this link
有关 ToString 方法的更多信息,请访问此链接
回答by mezoid
You might not be able to get it as a DateTime object...but when you want to display it you can format it in the way you want by doing something like.
您可能无法将其作为 DateTime 对象获取...但是当您想显示它时,您可以通过执行类似操作以您想要的方式对其进行格式化。
myDateTime.ToString("M/d/yyyy") which gives 10/19/2009 for your example.
myDateTime.ToString("M/d/yyyy") 为您的示例提供 10/19/2009。
回答by ilustreous
You'll always get the time portion in a DateTime type.
您将始终获得 DateTime 类型的时间部分。
DateTime goodDateHolder = Convert.ToDateTime(DateTime.Now.ToShortDateString());
will give you today's date but will always show the time to be midnight.
会给你今天的日期,但总是显示时间是午夜。
If you're worried about formatting then you would try something like this
如果您担心格式化,那么您可以尝试这样的操作
goodDateHolder.ToString("mm/dd/yyyy")
to get the date in the format that you want.
以您想要的格式获取日期。
This is a good resource msdn-dateformat
这是一个很好的资源msdn-dateformat
回答by Violently Happy
DateTime is merely a UInt64 with useful and clever formatting wrapped around it to make it appear like a date plus a time. You cannot eliminate the time element.
DateTime 只是一个 UInt64,带有有用且巧妙的格式环绕它,使它看起来像一个日期加上一个时间。您无法消除时间元素。