0
votes

I have a class in my api that contains an id and DateTime property.

public class MyTestClass
    {
        public int id { get; set; }
        public DateTime dateTime { get; set; }

    }

I was wondering if there is a way to accept only a JSON string that has a specific date format. My problem is that the model binder will parse both "21-01-1991" and "01-21-1991".

sample API controller

public void Post(MyTestClass myTest)
        {
            DateTime x = myTest.dateTime;
        }

I want the api to return bad request in case the user sent the dateTime property in any format other than the iso 8601 format.

2
I think you might need to show your relevant code for the "model binder" (but not the entire program, just enough to show what method you are using). Also It's a bit hard to tell from your question, but I think you are expecting that the JSON string should represent a date in ISO 8601, but you are seeing values like "21-01-1991" and "01-21-1991" that the parser attempts to parse anyway.robbpriestley
I don't currently have a model binder, i tried to write one but i was thinking that there must be an easier way or a configuration (like the serialization settings) that allows specifying the date formats that can be parsed. I built a model binder for DateTime parameters but I couldn't get it working for classes that contain date time properties and I don't wanna create a binder specific for each class. I was searching for some global setting that can handle this.user2386390

2 Answers

0
votes

Without getting too distracted, for the moment, by such things as "model binding" and "global settings", let's look at a very simple solution.

Set some constant to a representation of the format you expect the string to take. Deserialize the data and store the JSON value as a string. Don't try to force the source string data to take any particular date format, ISO or otherwise. Then, when you need it, parse out the data into a DateTime object using a get property.

public class MyTestClass
{
    public const string DATETIME_FORMAT = "MM-DD-YYYY";
    public int id { get; set; }
    public string dateTime { get; set; }
    public DateTime dateTimeConverted
    {
        get
        {
            string[] parts = dateTime.Split('-');
            int year, month, day;

            if (DATETIME_FORMAT == "MM-DD-YYYY")
            {
                year = Int32.Parse(parts[2]);
                month = Int32.Parse(parts[0]);
                day = Int32.Parse(parts[1]);   
            }
            else if (DATETIME_FORMAT == "DD-MM-YYYY")
            {
                year = Int32.Parse(parts[2]);
                month = Int32.Parse(parts[1]);
                day = Int32.Parse(parts[0]); 
            }

            return new DateTime(year, month, day);
        }
    }
0
votes

I was able to get around this simply by defining the serialization setting date format. I'm not sure why that didn't work the first time i tried it.

I just added the below to the startup.cs in the .net core api

services.AddMvc()
        .AddJsonOptions(options =>
        {
            options.SerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ssZZZ";
            options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;                
        });