0

I have a Datetime object and I want to remove time part from it. I tried the following line of code but still auth.dob contains time. How can I just set 2017-01-01 in it?

auth.dob = Convert.ToDateTime("2017-01-01").Date;
9
  • 2
    It will have to just be a string. The DateTime struct is a for both parts..... See NodaTime if you want something better! Commented Jul 7, 2017 at 9:52
  • I don't know the API whatsoever - but this local date seems to be what you're looking for Commented Jul 7, 2017 at 9:54
  • 1
    A DateTime object has time properties. Its name implies this. Why do you think you need to do this? What are you actually trying to accomplish here? Commented Jul 7, 2017 at 9:54
  • what type is auth.dob? Commented Jul 7, 2017 at 9:57
  • How you check that auth.dob still contains time? Commented Jul 7, 2017 at 10:00

1 Answer 1

3

Sadly .Net does not include a type that only stores the date. Generally we use DateTime and set the time to midnight. Then when you output the value you just omit the Time when displaying it see DateTime.ToString(string), DateTime.ToShortDateString() and DateTime.ToLongDateString().

While this works it does lead to all sorts of confusion. I've often felt it would have been better to have a Date struct a Time struct and a DateTime struct that was a composition of the other two. But sadly that isn't the case.

There is the Noda Time library, but that is probably overkill in this case.

Alternatively you can role your own and store the day, month and year as separate numeric variables.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.