0

I'm using .net Core 2.1, For serializing dates i'm using newtonsoft, Client side i'm using moment and knockout

So how do you cleanly go about setting up your site so dates can be sent back and forth and also always displayed in a friendly format?

I want the display to always be a variant of dd/MM/yyyy HH:mm:ss. Sometimes I will not be displaying the time.

Ultimately whenever i get or send a date back i get it in UK and the other end sees it as US (MM/dd/yyyy) etc.

I'm happy to work with dates on the server as ISO8601, i'm just stumped having to create new moments everywhere, specify incoming and out going formats all over the place. I'm clearly not doing it right.

These are my current serializer settings.

        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.Culture = new System.Globalization.CultureInfo("en-GB");
        settings.DateFormatString = "dd/MM/yyyy HH:mm:ss";
        settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        JsonConvert.DefaultSettings = () => settings;

I'm just not succeeding in keeping the format consistent.

user174634
  • 143
  • 10
  • `JsonConvert.DefaultSettings` is for console apps. If you are using [tag:asp.net-core] you set default options by using [`AddJsonOptions()`](https://stackoverflow.com/q/46053995/3744182). See e.g. [How to add CamelCasePropertyNamesContractResolver in Startup.cs?](https://stackoverflow.com/q/26393466/3744182) and [JsonSerializerSettings and Asp.Net Core](https://stackoverflow.com/q/35772387/3744182). – dbc Apr 04 '18 at 17:44
  • If I change those settings it affects the serialization across the entire site so maybe they improved it in later version of .netcore. I've actually just left the serializer settings as above and used moment on the page whenever I want to strip out the time and it's currently working – user174634 Apr 05 '18 at 12:39
  • Well you're setting the **global** `JsonSerializerSettings` so it appeared you wanted to modify them everywhere. If you want to customize your settings for a specific request try [ASP.NET Core API JSON serializersettings per request](https://stackoverflow.com/q/44828302/3744182). – dbc Apr 05 '18 at 17:55
  • Yes I did want to do it everywhere, when you said default settings is for console apps it threw me cause it's also working for the website as a whole. I have settled with using the settings as they are and using moment on my views to format accordingly. Thanks for your help I read every item posted – user174634 Apr 06 '18 at 11:16

0 Answers0