0
var travelDate = model.TravelDate.Value.AddHours(12).ToUniversalTime().Date;
var travelDate2 = travelDate.Date;

Why is Date still coming back as {6/20/2019 12:00:00 AM} and not {6/20/2019}. Is there a way for me to just get the date?

Hawkzey
  • 1,088
  • 1
  • 11
  • 21
  • The `Date` property is of type `DateTime`. It has to include _some_ time information. – Lance U. Matthews Jun 19 '19 at 18:34
  • A `DateTime` struct always has a time component, and the `Date` property returns a `DateTime` with a default time value of all zeros (midnight). Just ignore the time if you only care about the date. If the time portion is showing up in the UI somewhere, that's due to a formatting issue. See [Standard date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings) and [Custom date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/cust]om-date-and-time-format-strings) – Rufus L Jun 19 '19 at 18:35
  • Those are different _textual_ representations of the same `DateTime` instances. – Soner Gönül Jun 19 '19 at 18:37
  • @RufusL I have changed it so travelDate is now a string, and i need to get just the date part and not the time, as the api im calling does not allow for the time section.However it still gives me the time? – Hawkzey Jun 19 '19 at 18:42
  • If you have a string representation of a `DateTime` and you want to get just the `Date` part as a string, you can use a combination of `DateTime.Parse` to convert the string back to a `DateTime` struct, and then use string formatting from the links in my comment above to format that as a string without the time portion. For example: `var dateString = DateTime.Parse(dateTimeString).ToShortDateString();` – Rufus L Jun 19 '19 at 18:45

0 Answers0