C# 日期与日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/798121/
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
Date vs DateTime
提问by MrM
I am working on a program that requires the date of an event to get returned.
我正在开发一个需要返回事件日期的程序。
I am looking for a Date
, not a DateTime
.
我正在寻找一个Date
,而不是一个DateTime
。
Is there a datatype that returns just the date?
是否有只返回日期的数据类型?
采纳答案by Ronald Wildenberg
No there isn't. DateTime
represents some point in time that is composed of a date and a time. However, you can retrieve the date part via the Date
property (which is another DateTime
with the time set to 00:00:00
).
不,没有。DateTime
表示由日期和时间组成的某个时间点。但是,您可以通过Date
属性(这是另一个DateTime
时间设置为00:00:00
)检索日期部分。
And you can retrieve individual date properties via Day
, Month
and Year
.
回答by Mikko Rantanen
You can return DateTime where the time portion is 00:00:00 and just ignore it. The dates are handled as timestamp integers so it makes sense to combine the date with the time as that is present in the integer anyway.
您可以返回时间部分为 00:00:00 的 DateTime 并忽略它。日期作为时间戳整数处理,因此将日期与时间结合起来是有意义的,因为无论如何都存在于整数中。
回答by Jonathan Rupp
Unfortunately, not in the .Net BCL. Dates are usually represented as a DateTime object with the time set to midnight.
不幸的是,不在 .Net BCL 中。日期通常表示为时间设置为午夜的 DateTime 对象。
As you can guess, this means that you have all the attendant timezone issues around it, even though for a Date object you'd want absolutely no timezone handling.
您可以猜到,这意味着您有所有伴随的时区问题,即使对于 Date 对象,您绝对不需要时区处理。
回答by Andrew Hare
You could try one of the following:
您可以尝试以下方法之一:
DateTime.Now.ToLongDateString();
DateTime.Now.ToShortDateString();
But there is no "Date" type in the BCL.
但是 BCL 中没有“日期”类型。
回答by stevehipwell
The Date type is just an alias of the DateTime type used by VB.NET (like int becomes Integer). Both of these types have a Date property that returns you the object with the time part set to 00:00:00.
Date 类型只是 VB.NET 使用的 DateTime 类型的别名(就像 int 变成了 Integer)。这两种类型都有一个 Date 属性,可以返回时间部分设置为 00:00:00 的对象。
回答by C. Ross
回答by Sam Meldrum
For this, you need to use the date, but ignore the time value.
为此,您需要使用日期,但忽略时间值。
Ordinarily a date would be a DateTime with time of 00:00:00
通常,日期将是带有时间的 DateTime 00:00:00
The DateTime
type has a .Date
property which returns the DateTime
with the time value set as above.
该DateTime
类型具有一个.Date
属性,该属性返回DateTime
具有如上设置的时间值的 。
回答by hmcclungiii
The DateTime object has a Property which returns only the date portion of the value.
DateTime 对象有一个属性,它只返回值的日期部分。
public static void Main()
{
System.DateTime _Now = DateAndTime.Now;
Console.WriteLine("The Date and Time is " + _Now);
//will return the date and time
Console.WriteLine("The Date Only is " + _Now.Date);
//will return only the date
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
回答by ?yvind Skaar
Create a wrapper class. Something like this:
创建一个包装类。像这样的东西:
public class Date:IEquatable<Date>,IEquatable<DateTime>
{
public Date(DateTime date)
{
value = date.Date;
}
public bool Equals(Date other)
{
return other != null && value.Equals(other.value);
}
public bool Equals(DateTime other)
{
return value.Equals(other);
}
public override string ToString()
{
return value.ToString();
}
public static implicit operator DateTime(Date date)
{
return date.value;
}
public static explicit operator Date(DateTime dateTime)
{
return new Date(dateTime);
}
private DateTime value;
}
And expose whatever of value
you want.
并公开value
您想要的任何内容。
回答by Clay
I created a simple Date structfor times when you need a simple date without worrying about time portion, timezones, local vs. utc, etc.
当您需要一个简单的日期而不用担心时间部分、时区、本地与 UTC 等时,我创建了一个简单的Date 结构。
Date today = Date.Today;
Date yesterday = Date.Today.AddDays(-1);
Date independenceDay = Date.Parse("2013-07-04");
independenceDay.ToLongString(); // "Thursday, July 4, 2013"
independenceDay.ToShortString(); // "7/4/2013"
independenceDay.ToString(); // "7/4/2013"
independenceDay.ToString("s"); // "2013-07-04"
int july = independenceDay.Month; // 7