-1

In an ASP.NET MVC application, I get the date from the database in this way:

public class MonthController : ApiController

{
    public List<Month> monthContainer;

    public List<Month> getMonth()
    {
        DataContext dataContext = new DataContext();

        monthContainer = (from d in dataContext.Duties
                          select new Month()
                          {
                              MonthDate = d.Date
                          }).ToList();
        return monthContainer;
    }

In the answer comes a date in this format:

{
    "MonthDate": "2015-10-01T00:00:00"
},
{
    "MonthDate": "2015-10-02T00:00:00"
}

And so it goes every day of the month MonthDate until a certain year. How can I withdraw only months in this format without doubles? How to make a change in the controller? I want to get this format:

    "MonthDate": "2015-10"
    "MonthDate": "2015-11"
    "MonthDate": "2015-12"
    "MonthDate": "2016-01"
... "MonthDate": "2019-12"
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
just
  • 1
  • 5
  • Dates have no format. They are binary values. Formats are used *only* when text is parsed into a date, or a date is formatted as a text for display. Wherever you display those dates, add a format string or a call to `ToString` or `String.Format` with the format you want – Panagiotis Kanavos Nov 21 '19 at 10:27
  • How do you display those dates? That's where you need to use formatting. At the very least post the relevant view code – Panagiotis Kanavos Nov 21 '19 at 10:27
  • You can use the [DisplayFormat](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.displayformatattribute?view=netframework-4.8) attribute to tell MVC how to format specific fields. You could add `[DisplayFormat(DataFormatString = "{0:yyyy-MM}")]` to the `MonthDate` property – Panagiotis Kanavos Nov 21 '19 at 10:33
  • You may need to change `MonthDate` property of the `Month` class to string type. then you may use the following code. monthContainer = (from d in dataContext.Duties select new Month() { MonthDate = d.Date.ToString("yyyy-MM", CultureInfo.InvariantCulture) }).ToList(); because `date` type does not have any format it is a binary value which you may format when converting to string. – Mukul Keshari Nov 21 '19 at 10:33
  • @MukulKeshari there's no reason for this. All UI stacks, whether web or desktop, offer ways to format data. – Panagiotis Kanavos Nov 21 '19 at 10:34
  • Guys, I write only the backend and want to format the date in the controller. Model - public class Month { public DateTime MonthDate { get; set; } } – just Nov 21 '19 at 10:44
  • @just did you try adding the `DisplayFormat` attribute ? That's the easiest way for the backend to affect how the *front*end renders data. – Panagiotis Kanavos Nov 21 '19 at 10:53
  • @just wait, are you asking about *JSON serialization* in Web API? That's a *very* different question, already supported by JSON.NET through custom converters. The second duplicate shows how to create a custom date converter. Change the `DateTimeFormat` to `yyyy-MM` and add `[Newtonsoft.Json.JsonConverter(typeof(CustomDateTimeConverter))]` to the `MonthDate` property. The result will be `{"MonthDate":"2019-11"}` – Panagiotis Kanavos Nov 21 '19 at 11:03
  • @Panagiotis Kanavos, I get a response in JSON, sorry, not indicated. I do not quite understand how to apply the solution with [Newtonsoft.Json.JsonConverter (typeof (CustomDateTimeConverter))] – just Nov 21 '19 at 11:10
  • Did you read the duplicate? Create the converter class shown there, use the custom format and apply the converter. You'll find more examples at [JSON.NET's doc site](https://www.newtonsoft.com/json/help/html/JsonConverterAttributeClass.htm). JSON.NET converts data to JSON and vice versa using converters. You can create your own converters to change how the conversion works, use different formats etc. – Panagiotis Kanavos Nov 21 '19 at 11:11
  • 1
    @Panagiotis Kanavos, hi! Solution from a duplicate helped me, I created new classes in the model, and they converted my JSON, thx u! – just Nov 22 '19 at 06:04

2 Answers2

-1

Please, use DateTime.ParseExact like this :

DateTime.ParseExact(MonthDate.ToString(), "yy/MM/dd HH:mm:ss", 
    CultureInfo.InvariantCulture
    ).ToString("yyyy-MM")
ChillAndCode
  • 188
  • 2
  • 12
-1

you have to use parseExact method like:

MonthDate = DateTime.ParseExact(d.Date, "yyyy-MM",CultureInfo.InvariantCulture);

Some Documentation: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=netframework-4.8

Pugnatore
  • 395
  • 3
  • 19