0

I have a .net core web api where return a data as below when call from xamarin forms

{"id":1,"customer":"XXX Pty Ltd","salesman":"Fred","shipmentDate":"2020-01-08T00:00:00"}

I use Jsonconverter.DeserializeObject method to covert to .Net object, the code as below

var result = JsonConvert.DeserializeObject<Logis[]>(returnJson);

The date is in wrong format. It become 01/08/2020:00:00:00. How I can convert it to 08/01/2020 00:00:00?

My .Net object as below

      [JsonProperty("Id")]
    public int Id { get; set; }

    [JsonProperty("Customer")]
    public string Customer { get; set; }

    [JsonProperty("Salesman")]
    public string Salesman { get; set; }

    [JsonProperty("ShipmentDate")]        
    public DateTime ShipmentDate { get; set; }

[Edit]
In my WebAPI, I modify my ViewModel class by add a string property for the date and format it to the format I needed. I not sure the way I do is correct or not but I get the result I needed.

 public class ViewModel
{
    public int Id { get; set; }

      ............

    public DateTime ShipmentDate { get; set; }

    public string ShipmentDateString
    {
        get
        {
            return ShipmentDate.ToString("dd-MM-yyyy");
        }
    }
}

In xamarin forms, I bind it using ShipmentDateString property instead of ShipmentDate

Bubble Bub
  • 651
  • 4
  • 12
  • 32
  • 2
    No it didn't. A `DateTime` doesn't have a format, whatever you're using to display it put it in that format. If you want to see it in a specific way, you need to convert it to a string with the localization and/or format specified. – iakobski Feb 05 '20 at 07:42
  • I am not sure what are you talking about because DateTime as an object does not have a format, Is it that your receiver API receives it as a string and then tries to parse it or something? – FreakyAli Feb 05 '20 at 07:44
  • Please look [here](https://stackoverflow.com/questions/18635599/specifying-a-custom-datetime-format-when-serializing-with-json-net), maybe this will answer your need – Dani Toker Feb 05 '20 at 07:51
  • Does this answer your question? [C# DateTime to "YYYYMMDDHHMMSS" format](https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – Drag and Drop Feb 05 '20 at 08:24
  • @iakobski thanks for the direction. I just convert the object to string data type and format it in the API before return to client. Thanks. Here is the similar question https://stackoverflow.com/questions/26509140/datetimes-deserializing-wrong-jsonconvert-is-returning-wrong-dates – Bubble Bub Feb 05 '20 at 08:41
  • @BubbleBub Show us the code where you do that - the problem is not in the code you've shown above. – iakobski Feb 06 '20 at 07:54
  • @iakobski I add a new string property for the date in my ViewModel in WebAPI and format it to the date format I needed. – Bubble Bub Feb 20 '20 at 07:13

4 Answers4

2

you could try to this method:

public class DateFormatConverter : IsoDateTimeConverter
{
    public DateFormatConverter(string format)
    {
        DateTimeFormat = format;
    }
}

And use it this way:

[JsonConverter(typeof(DateFormatConverter), "dd/MM/yyyy")]  //accordinga to specific requirements    
public DateTime ShipmentDate { get; set; }
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • Not clear why this has been upvoted, the OP has been able to deserialize into a DateTime without any issue. He's asking about a format m/d/y versus d/m/y – iakobski Feb 06 '20 at 07:53
  • this is a example code,you could defined the fomat accord your need,it is just one idea – Leo Zhu Feb 06 '20 at 08:00
  • Yes, it's a fine example, but it doesn't address the OP's problem. – iakobski Feb 06 '20 at 08:04
0

See this answer with custom dateTime format class appended as tag on your DateTime property

[JsonProperty("ShipmentDate")]        
public DateTime ShipmentDate { get; set; }
Max
  • 31
  • 5
-1

You can either set the correct culture on the environment you're running on (if you are complaining that the debugger is showing the format you don't like), or you can specify the format directly:

Console.WriteLine(ShipmentDate.ToString("dd/MM/yyyy HH:mm:ss"));
iakobski
  • 1,000
  • 7
  • 8
-1

You can convert it to your specified Date time format while Deserialize it:

 var format = "dd/MM/yyyy"; // your datetime format
 var dateTimeConverter = new IsoDateTimeConverter { DateTimeFormat = format };
 var result = JsonConvert.DeserializeObject<Logis[]>(returnJson,dateTimeConverter);
MShah
  • 1,247
  • 9
  • 14