C# Data type for Birthdate
Asked Answered
R

3

5

There is no Date type. If I want an object that is only a date with no time, like a birthdate, what type would I use?

I'm sure this question has been asked and answered already, but I'm not finding this exact question, only lots about date and datetime in general.

Thanks!

(Edit: I am not asking how to remove the date, or the time, from a DateTime. I was hoping that there might be a data type that stores ONLY date, and not a date and a time together. The answer, it seems, is No.)

Ravenous answered 8/7, 2015 at 17:19 Comment(1)
Well, it's obvious that you only want the date part of a datetime so I suggest you use the Date property of the DateTime struct. For more info, see this dupe link: How to remove time portion of date in C# in DateTime object only?Mean
A
4

There is no just Date type, use the DateTime data type and only set the date, the time will be all 0s.

Appal answered 8/7, 2015 at 17:20 Comment(2)
@CaseyCrookston - you could make your own Date struct if you really want one.Capwell
you could, yes. and then you could write all of the infrastructure around DateTime and DateTimeOffset, and handle all the calendars and daylight savings, etc. but why would you, when someone's already done all that work?Repressive
M
4

Update: DateOnly (and TimeOnly) types (Structures) have been added to .NET 6.

See: How to use the DateOnly and TimeOnly structures.

Materfamilias answered 14/6 at 6:39 Comment(0)
D
1

There not is an only Date type... Use Date type, and set only the date, and to get only the date use ex: DateTime.Now.Date

Diecious answered 8/7, 2015 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.