1

Let's say I have the following DateTime which has a value of: "2017-01-27T00:00:00".

How can I remove the above date to return only the date without the time and without changing the format? i.e return "2017-01-27"

16
  • You need to convert to a string. Commented Jan 27, 2017 at 10:10
  • You can change the way properties are serialized with the proper attribute. Where does the DateTime value come from? Is it a property ? Commented Jan 27, 2017 at 10:15
  • In addition, do you know it will always be - as the separator? And obviously you want to return a string, yar? Commented Jan 27, 2017 at 10:16
  • @musefan that's the ISO8601 standard. The separator will always be - Commented Jan 27, 2017 at 10:17
  • @erbsoftware are you using JSON.NET or MVC's older serializer? Commented Jan 27, 2017 at 10:17

2 Answers 2

3

Something like this maybe?

var dt = DateTime.Parse("2017-01-27T00:00:00");
return dt.ToString("yyyy-MM-dd");
Sign up to request clarification or add additional context in comments.

1 Comment

A far better option is to tell the serializer to use that format. That's already possible with Json.NET and there are a lot of duplicates that explain this
0

If you need some customization for the date, you can use *name*.ToString('yyyy-MM-dd') to customize the result. See the documentation here

1 Comment

ToShortDateString() returns the local short date format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.