This is a very simple trick to handle JSON date format on AspNet Core by global settings. This can be applicable for the older version as well.
In a newer version by default, .Net depends upon Newtonsoft to process any JSON data. Newtonsoft depends upon Newtonsoft.Json.Converters.IsoDateTimeConverter class for processing date which in turns adds timezone for JSON data format.
There is a global setting available for same that can be adjusted according to requirement. So, for example, we want to set default formatting to US format, we just need this code.
In a newer version by default, .Net depends upon Newtonsoft to process any JSON data. Newtonsoft depends upon Newtonsoft.Json.Converters.IsoDateTimeConverter class for processing date which in turns adds timezone for JSON data format.
There is a global setting available for same that can be adjusted according to requirement. So, for example, we want to set default formatting to US format, we just need this code.
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.DateTimeZoneHandling = "MM/dd/yyyy HH:mm:ss";
});
Just to point out, DateTimeZoneHandling is an enumeration, you actually need to set DateFormatString
ReplyDeleteLearning Python Full Stack Development provides practical skills for designing, developing, testing, and deploying modern web applications. <a href="https://www.javascripttrainingcourses.com/full-stack/dotnet-full-stack/aspnet-core-nextjs-full-stack-course" title="ASP.NET Core Next.JS Full Stack Develo…From creating attractive web pages and connecting databases to developing REST APIs and implementing backend business logic, Python Full Stack training covers the complete application development process. It is suitable for beginners as well as developers who want to strengthen their skills and build real-world web projects using Python and modern web technologies.
ReplyDelete