My application consumes the APIs, one of the API returns the Datetime value with unix time format
ex: user.DateOfBirth = "/Date(476197200000+1100)/"
The deserialize process is working well (deserialize<User>
object) but when I display the datetime to GUI.
the UI displays this date as 2/Feb/1985 My expected is: 3/Feb/1985
My local environment displays correctly, it's 3/Feb/1985, but on UAT environment, it display less than 1 day (2/Feb/1985)
I'm using Newtonsoft.Json v10.x.x
James Z
12.3k10 gold badges27 silver badges48 bronze badges
1 Answer 1
Not sure how you are deserializing but you might want to look into how to parse using specific time zone.
You can try this.
microsoftDateFormatSettings =
new { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Local};
var items = JsonConvert.DeserializeObject<List<lstObject>>.
(jsonString, microsoftDateFormatSettings);
4 Comments
QuachNguyen
Hello Rushikesh Vyas Thanks for your reply, yes, I have been serializing as your above suggestion, I also add ateTimeZoneHandling.Local but not success My Json is: "DateOfBirth":"\/Date(784731600000+1100)\/" My Customer has: [Description("The contacts date of birth.")] [DataMember(IsRequired = false)] public DateTime? DateOfBirth { get; set; } Could you help me in this case? Thank you
Rush
Can you show the snippet of your code?that’d be a great help.
QuachNguyen
It's exactly the same like you, the difference is using <T> object instead of List<object>, and microsoftDateFormatSettings change to your suggestion above Thank you
Rush
So do you see the correct value when you deserialize and UI control is showing wrong date? Or when you deserialize json you are getting incorrect information