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

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

Date vs DateTime

c#.netasp.net

提问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. DateTimerepresents some point in time that is composed of a date and a time. However, you can retrieve the date part via the Dateproperty (which is another DateTimewith the time set to 00:00:00).

不,没有。DateTime表示由日期和时间组成的某个时间点。但是,您可以通过Date属性(这是另一个DateTime时间设置为00:00:00)检索日期部分。

And you can retrieve individual date properties via Day, Monthand Year.

您可以通过Day,Month和检索单个日期属性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

DateTime has a Dateproperty that you can use to isolate the date part. The ToStringmethod also does a good job of only displaying the Date part when the time part is empty.

DateTime 有一个Date属性,您可以使用它来隔离日期部分。该的ToString方法也做的到时部分是空的只是显示的日期部分做好。

回答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 DateTimetype has a .Dateproperty which returns the DateTimewith 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 valueyou 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

https://github.com/claycephus/csharp-date

https://github.com/claycephus/csharp-date