开发者

Is there only a Date type like TimeSpan instead of DateTime?

开发者 https://www.devze.com 2023-04-13 07:31 出处:网络
In my application I have a Movie type and User type where any User can specify when they saw a particular movie in their logs along with ratings, etc, but I am开发者_运维百科 only interested in the da

In my application I have a Movie type and User type where any User can specify when they saw a particular movie in their logs along with ratings, etc, but I am开发者_运维百科 only interested in the day not the time, so was wondering if there is only a Date type that stores only the day and nothing else, i.e. no Time.


There is not. Closest thing, if you must ensure there's no time data attached to your DateTime object, is to use the DateTime.Date property, whose return value is another DateTime, but with the time value set to 00:00:00 (midnight).

And of course, when displaying the date, you can control its formatting as you like.

In particular, the Short Date specifier:

var dateValue = new DateTime(2011, 10, 14);
dateValue.ToString("d");

Will output 10/14/2011 in the Invariant culture.


You typically use a DateTime with the time set to midnight. You can turn any time stamp into a date using the DateTime.Date property.

If you need a more compact representation you could calculate an integer that represents the date as number of days since a certain day, but I'd only do this if it's really necessary.

But DateTime is a bit ugly for local times(It's OK for UTC times), so you could consider working with DateTimeOffset or alternatively use a third party library.


No, there isn't. You can use DateTime, but I don't recommend it. It's fundamentally the wrong type for the job, and it has some very odd characteristics.

I'm busy working on Noda Time, an alternative date and time library for .NET based on the calculation engine of Joda Time but with a mostly-from-scratch API. It's not fully production ready yet, but it's getting there - and I'd be interested in giving as much assistance as necessary to early adopters...

Of course, we support LocalDate :) (It doesn't just store the date - it also stores the calendar system it's using, so that if you're not using the common Gregorian calendar, you can still perform operations in your natural system, but that's a different matter.)


Use the DateTime constructor that takes year, month and day:

public DateTime(
      int year,
      int month,
      int day
  )

E.g.

DateTime usersDate = new DateTime(2011, 10, 14);

Note that this still uses the same type, but with a zero time (midnight).


In the BCL, no. But you can use the various ToString overrides to display (return a string) that contains just a date and ignores the time portion.


No. But you could create one, of you need it (based on DateTime)


Not as far as I'm aware, but you can always just refer to the Date portion of the DateTime object.

Like this: DateTime myDate = DateTime.Now.Date;


This is what DateTime.Date is for. It's still a DateTime, but it zeroes the time for you. Just use this consistently and you'll get what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号