日期与日期时间

我正在开发一个程序,它需要一个事件的日期才能返回。

我要找的是 Date不是 DateTime

有没有只返回日期的数据类型?

253926 次浏览

可以返回时间部分为00:00:00的 DateTime 并忽略它。日期作为时间戳整数进行处理,因此将日期与时间合并在一起是有意义的,因为时间无论如何都存在于整数中。

不幸的是。基本建设总计划网。日期通常表示为时间设置为午夜的 DateTime 对象。

正如您可以猜到的那样,这意味着在它周围存在所有相关的时区问题,即使对于 Date 对象,您绝对不希望处理任何时区问题。

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

您可以通过 DayMonthYear检索各个日期属性。

更新 : 在.NET 6中,类型 DateOnlyTimeOnly 介绍仅表示一个日期或一个时间。

你可以试试下面的方法:

DateTime.Now.ToLongDateString();
DateTime.Now.ToShortDateString();

但是在 BCL 中没有“ Date”类型。

Date 类型只是 VB.NET 使用的 DateTime 类型的别名(就像 int 变成了 Integer)。这两种类型都具有 Date 属性,该属性返回时间部分设置为00:00:00的对象。

DateTime 有一个 日期属性,可用于隔离日期部分。ToString方法还可以很好地只在时间部分为空时显示 Date 部分。

为此,您需要使用日期,但忽略时间值。

通常一个日期应该是一个时间为 00:00:00的 DateTime

DateTime类型具有 .Date属性,该属性返回设置为上述时间值的 DateTime

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);
}

创建一个包装类,如下所示:

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;
}

你想曝光什么就曝光什么。

我创建了一个简单的 日期结构,当你需要一个简单的日期,而不用担心时间部分,时区,本地与 UTC 等。

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

没有 Date数据类型。

但是,您可以使用 DateTime.Date来获取日期。

脑电图。

DateTime date = DateTime.Now.Date;
public class AsOfdates
{
public string DisplayDate { get; set; }
private DateTime TheDate;
public DateTime DateValue
{
get
{
return TheDate.Date;
}


set
{
TheDate = value;
}
}
}

看起来。NET6最终引入了一个只有日期的类型。它将被称为 DateOnly,并且在 System名称空间中将有一个 TimeOnly类型添加到 BCL 中。

它已经可以在预览4。阅读 这篇博客文章了解更多细节。

正如 Dejan 和 Jonas Lomholdt 所指出的,

.Net 6具有 DateOnly类型,它是一种仅用于表示日期(如年、月和日)的结构。

DateOnly d1 = new DateOnly(2022, 5, 16);
Console.WriteLine(d1);            // 5/16/2022
Console.WriteLine(d1.Year);      // 2021
Console.WriteLine(d1.Month);     // 5
Console.WriteLine(d1.Day);       // 16
Console.WriteLine(d1.DayOfWeek); // Monday






// Manipulation
DateOnly d2 = d1.AddMonths(3);  // You can add days, months, or years. Use negative values to subtract. We are adding 3 months
Console.WriteLine(d2);     // "8/16/2022"  notice there is NO time


// You can use the DayNumber property to find out how many days are between two dates
int days = d2.DayNumber - d1.DayNumber;
Console.WriteLine($"There are {days} days between {d1} and {d2}");  //There are 92 days between 5/16/2022 and 8/16/2022

这篇文章的全部功劳归于马特 · 约翰逊-品脱(Matt Johnson-Pint) :

Https://devblogs.microsoft.com/dotnet/date-time-and-time-zone-enhancements-in-net-6/