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"

erbsoftware
  • 365
  • 1
  • 3
  • 13
  • You need to convert to a `string`. –  Jan 27 '17 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 ? – Panagiotis Kanavos Jan 27 '17 at 10:15
  • In addition, do you know it will always be `-` as the separator? And obviously you want to return a string, yar? – musefan Jan 27 '17 at 10:16
  • @musefan that's the ISO8601 standard. The separator will *always* be `-` – Panagiotis Kanavos Jan 27 '17 at 10:17
  • @erbsoftware are you using JSON.NET or MVC's older serializer? – Panagiotis Kanavos Jan 27 '17 at 10:17
  • @PanagiotisKanavos: The separator can be whatever the f*** you want it to be. Requirements dictate that, and that is what I am looking for the OP to clear up – musefan Jan 27 '17 at 10:18
  • @musefan dates in Json follow the ISO8601 format. Of course, if business requirements state `Return the date in an unrecognizable format that will fail to parse outside our company without manual intervention` ... – Panagiotis Kanavos Jan 27 '17 at 10:20
  • @PanagiotisKanavos: What makes you think anyone needs to parse it? Sometimes I will have a ajax request return JSON and just put the string in the format I want to display it in, because there isn't a need to work with the value other than display it on the screen. Besides, I have seen plenty more questionable requirements from SO questions... – musefan Jan 27 '17 at 10:22
  • @erbsoftware Sorry for closing this as duplicate but I wanted to stop the flood of bad answers. The duplicate answers how to use a date-only format when using Json.NET, the most common case nowadays. If you use MVC's older (obsolete) serializer, there's bound to be another duplicate that explains how to do it – Panagiotis Kanavos Jan 27 '17 at 10:22
  • @musefan because JavaScript actually understands ISO8601? And because display formats are the job of the displaying page, not the API? – Panagiotis Kanavos Jan 27 '17 at 10:24
  • @PanagiotisKanavos, OP has not tagged this Json.NET (just asp.net-mvc) so most likely they are not using Json.NET! –  Jan 27 '17 at 10:24
  • @StephenMuecke I explained this in a previous comment. There's a flood of bad answers. Pedro's answer at least provides the correct format. I'd gladly change the duplicate if the OP is using MVC's serializer – Panagiotis Kanavos Jan 27 '17 at 10:26
  • @PanagiotisKanavos: Again with the assumptions... who said anything about an API? Could just be a controller action that is used for ajax loading of data. Why bother giving the client the task of working out how to display a date, when you can just send back a string exactly how you want it displayed. – musefan Jan 27 '17 at 10:28
  • @musefan OK you win. World loses. JSON doesn't mean JSON anymore – Panagiotis Kanavos Jan 27 '17 at 10:28
  • @PanagiotisKanavos: You work with the requirements you have, maybe you are right, maybe the OP *does* need to parse the date client-side. But to say it *has* to be done that way is way too restrictive – musefan Jan 27 '17 at 10:31
  • @PanagiotisKanavos: So are you saying JSON doesn't support strings? – musefan Jan 27 '17 at 10:31

2 Answers2

3

Something like this maybe?

var dt = DateTime.Parse("2017-01-27T00:00:00");
return dt.ToString("yyyy-MM-dd");
Pedro Luz
  • 973
  • 5
  • 14
  • 3
    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 – Panagiotis Kanavos Jan 27 '17 at 10:21
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

Dr. Roggia
  • 1,095
  • 3
  • 16
  • 40