Send date from my front end (angular) to c# .net datetime (webapi)

Total Post:27

 3099  View(s)
Rate this:

On my front-end I have a HTML input box where the user can enter a date. This adds data to my AngularJS modal and I then try to send it to the server as a JSON object when the user clicks save.

On the server I have used this which I think modifies the way dates are expected. All I know is that it allows AngularJS to nicely read the dates coming from the back-end.

        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        json.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mmZ" });

Does anyone have any ideas how I can take a date entered into the input box in a format such as YYYY-MM-DD, modify in some way and then pass this to the server and have the server read it as a datetime?

The problem I have is that I am not sure how I can modify the YYYY-MM-DD date. When I try entering in a date like "2011-12-31" it does not get accepted by the server and the column that it should be entered into shows a null value for the date.

  1. Post:412

    Re: Send date from my front end (angular) to c# .net datetime (webapi)

    Have you ever tried to create a subclass of IsoDateTimeConverter? Try this:

    public class MyJsonDateTimeConverter : IsoDateTimeConverter
        public MyJsonDateTimeConverter()
            base.DateTimeFormat = "yyyy-MM-dd";

    Then use it in place of the last line:

    json.SerializerSettings.Converters.Add(new MyJsonDateTimeConverter());

    Hope this helps :)

      Modified On Apr-07-2018 12:12:58 AM



Enter your email address here always to be updated. We promise not to spam!