.NET Date without Time - 有没有,或者我不需要吗?

发布于 2024-12-01 05:09:09 字数 455 浏览 1 评论 0原文

我正在做一些域建模,并遇到一个属性,在我看来,最好将其公开为日期而不是日期时间,没有时间组件。

框架中没有这种类型是否有充分的理由?向 SQL Server 添加日期类型被认为是一个足够好的主意。另外,如果有人知道 Date 类的方便实现,请告诉我!

2018 年编辑:我现在使用值对象类型来解决这个问题:

public class Date : ValueOf<DateTime, Date>  //Install-Package ValueOf
{ 
    protected override void Validate() 
    { 
        if (Value.Date != Value) throw new ArgumentException(); 
    } 
}

...

var date = Date.From(someDateTime);

I'm doing some domain modelling and coming across a property which to my mind would be best exposed as a Date rather than a DateTime, without a time component.

Is there a good reason why there is no such type in the Framework? It was deemed a good enough idea to add a Date type to SQL Server. Also, if someone knows of a handy implementation of a Date class, please let me know!

2018 edit: I now tackle this using a Value Object Type:

public class Date : ValueOf<DateTime, Date>  //Install-Package ValueOf
{ 
    protected override void Validate() 
    { 
        if (Value.Date != Value) throw new ArgumentException(); 
    } 
}

...

var date = Date.From(someDateTime);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

面如桃花 2024-12-08 05:09:09

提问时还没有这样的人,是的,应该有一个。幸运的是,从 .NET 6 开始,我们有了 DateOnlyTimeOnly

我不相信除此之外还有什么好的理由 在我(非常有偏见的)看来,.NET 中的日期和时间 API 不是很好

我是主要维护者的 Noda Time 项目有 LocalDateLocalTime,作为相当大的类型集的一部分。

There wasn't one at the time the question was asked, and yes, there should be one. Fortunately, as of .NET 6 we have DateOnly and TimeOnly.

I don't believe there was a good reason other than that the date and time API in .NET isn't very good in my (very biased) view.

The Noda Time project of which I'm the primary maintainer has LocalDate and LocalTime, as part of a rather larger set of types.

昇り龍 2024-12-08 05:09:09

当您需要一个简单的日期而不用担心时间部分、时区时,我创建了一个简单的 Date struct 、本地与 utc 等

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

I created a simple Date struct for times when you need a simple date without worrying about time portion, timezones, local vs. utc, etc.

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

离鸿 2024-12-08 05:09:09

在数据库中,存储容量更为重要,因此使用比日期时间更少空间的日期类型可能更有意义。在应用程序中,您很少需要保存这几个字节。

您可以轻松地创建一个在 DateTime 之间进行转换的 Date 类型,但仅在内部存储日期。像这样的东西:

public struct Date {

  private int _days;

  private Date(int days) { _days = days; }

  public static implicit operator DateTime(Date date) {
    return DateTime.MinValue.AddDays(date._days);
  }

  public static implicit operator Date(DateTime dateTime) {
    return new Date((int)(dateTime - DateTime.MinValue).TotalDays);
  }

}

In a database storage capacity is more important, so a date type that uses less space than a datetime may make sense. In an application you rarely have the need for saving those few bytes.

You could rather easily make a Date type that converts to and from DateTime, but only stores the days internally. Something like:

public struct Date {

  private int _days;

  private Date(int days) { _days = days; }

  public static implicit operator DateTime(Date date) {
    return DateTime.MinValue.AddDays(date._days);
  }

  public static implicit operator Date(DateTime dateTime) {
    return new Date((int)(dateTime - DateTime.MinValue).TotalDays);
  }

}
£冰雨忧蓝° 2024-12-08 05:09:09

使用 ToShortDateString() 方法。如果您需要网格或其他东西的属性,请创建一个仅返回 yourDate.ToShortDateString() 的只读属性,您就得到了它!

Use ToShortDateString() method. If you need a property for a grid or something, make a readonly property which just returns yourDate.ToShortDateString() and you got it!

橪书 2024-12-08 05:09:09

VB.NET 有这种类型,即Date 数据类型。我相信这是作为祖父 VB6 应用程序代码的要求,其中包含此类型而不是我们在 .NET 中使用的 DateTime 类型。

日期数据类型 (Visual Basic):
http://msdn.microsoft.com/en-我们/library/3eaydw6e(v=vs.110).aspx

VB.NET has this type, the Date data type. I believe this was here as a requirement for grandfathering VB6 app code which contained this type and not the DateTime type we use in .NET.

Date Data Type (Visual Basic):
http://msdn.microsoft.com/en-us/library/3eaydw6e(v=vs.110).aspx

小梨窩很甜 2024-12-08 05:09:09

是的,有一个。 DateDateTime (VB) 和 DateTime (C#) 都有一个名为 Date 的函数,仅返回日期/时间的日期部分。

还有一个 Time 函数,因此您可以将它们用于两者并分别返回日期/时间部分。

Yes, there is one. Date,DateTime (VB) and DateTime (C#) both have a function called Date on them that return only the date portion of the date/time.

There is also a Time function so you can use them for both and return the date/time parts separately.

◇流星雨 2024-12-08 05:09:09

实际上没有任何必要 - 只需使用时间部分设置为零(即前一天的午夜)的日期,或者完全忽略时间部分。

例如,您可以使用 DateTime.Today 获取今天的日期(而不是 DateTime.Now

There isn't really any need for one - just use a date with the time component set to zero (i.e. midnight of the previous day), or just ignore the time component completely.

For example you can use DateTime.Today to get today's date (as opposed to DateTime.Now)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文