Joan Venge
Joan Venge

Reputation: 330912

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

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.

Upvotes: 0

Views: 164

Answers (8)

CodesInChaos
CodesInChaos

Reputation: 108790

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.

Upvotes: 3

Geoff
Geoff

Reputation: 8850

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).

Upvotes: 1

harpo
harpo

Reputation: 43168

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.

Upvotes: 0

Tom Chantler
Tom Chantler

Reputation: 14931

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;

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500065

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.)

Upvotes: 2

Dan J
Dan J

Reputation: 16708

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.

Upvotes: 4

driis
driis

Reputation: 164281

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

Upvotes: 0

CodingGorilla
CodingGorilla

Reputation: 19842

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.

Upvotes: 0

Related Questions